zoukankan      html  css  js  c++  java
  • javaIO——LineNumberReader

      LineNumberReader 是java字符流中的一员,它继承自 BufferedReader,只是在 BufferedReader 基础上,提供了对当前流位置所在文本行的标记记录。先来看看定义:

        

      可以看出,其定义了一个 lineNumber 字段对当前所在行进行记录。注释中红框说明了:setLineNumber(int) 方法仅仅是改变从 getLineNumber() 返回的值而已,而不会改变流的当前位置。也就是说 lineNumber 只是一个记录值,并不影响流的读取过程。

      下面贴一下 read 方法:

        

        public int read() throws IOException {
            synchronized (lock) {
                int c = super.read();
                if (skipLF) {
                    if (c == '
    ')
                        c = super.read();
                    skipLF = false;
                }
                switch (c) {
                case '
    ':
                    skipLF = true;
                case '
    ':          /* Fall through */
                    lineNumber++;
                    return '
    ';
                }
                return c;
            }
        }
    
    
        public int read(char cbuf[], int off, int len) throws IOException {
            synchronized (lock) {
                int n = super.read(cbuf, off, len);
    
                for (int i = off; i < off + n; i++) {
                    int c = cbuf[i];
                    if (skipLF) {
                        skipLF = false;
                        if (c == '
    ')
                            continue;
                    }
                    switch (c) {
                    case '
    ':
                        skipLF = true;
                    case '
    ':      /* Fall through */
                        lineNumber++;
                        break;
                    }
                }
    
                return n;
            }
        }
    
    
    
        public String readLine() throws IOException {
            synchronized (lock) {
                String l = super.readLine(skipLF);
                skipLF = false;
                if (l != null)
                    lineNumber++;
                return l;
            }
        }

      可以看出,都是通过调用父类 BufferedReader 的 对应方法,然后根据对换行符的判断,进行行数的增长操作。值得注意的是,行数记录的是外部使用者真正获得数据的流位置所在的行,而不是缓存位置所在的行

      总结起来就是,LineNumberReader 是可以记录当前数据所在行号的字符缓冲输入流。

  • 相关阅读:
    网页设计的12种颜色
    深入理解编辑过程
    数据压缩
    <Mastering KVM Virtualization>:第四章 使用libvirt创建你的第一台虚拟机
    <Mastering KVM Virtualization>:第三章 搭建独立的KVM虚拟化
    <Mastering KVM Virtualization>:第二章 KVM内部原理
    <Mastering KVM Virtualization>:第一章 了解Linux虚拟化
    本地设置
    Spring Data JPA之删除和修改
    在Mac Chrome上关闭跨域限制--disable-web-security
  • 原文地址:https://www.cnblogs.com/coding-one/p/11377347.html
Copyright © 2011-2022 走看看