[Re-learning Java basics][Java IO stream][Part.4]Filtering character input and output streams

[Re-learning Java basics][JavaIO stream][Part.4]Filtering character input and output streams

FilterReader 与PushbackReader

Overview

FilterReader is an abstract class used to filter input characters PushbackReader is a concrete implementation of FilterReader

Official Notes

FilterReader is an abstract class that reads a stream of filtered characters. The default method of this abstract class allows all stream content to pass. Subclasses that inherit FilterReader must override some of these methods or add additional methods and properties (used to implement filtered reading)

PushbackReader allows rollback of character input streams inherited from FilterReader

Source code analysis

write picture description here

You can see that FilterReader basically does not have any of its own methods, all of which are inherited from Reader.

write picture description here

And the fallback stream has its own fallback method unread

Member variables

提供回滚的缓冲字符数组
private char[] buf;

缓冲数组游标
private int pos;

member method

Construction method

public PushbackReader(Reader in, int size) {
    super(in);
    if (size <= 0) {
        throw new IllegalArgumentException("size <= 0");
    }
    按size大小创建回退缓冲数组
    this.buf = new char[size];
    this.pos = size;
}


public PushbackReader(Reader in) {
    this(in, 1);
}

The fallback method is actually to buffer the read content into the buffered character array

 public int read() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (pos < buf.length)
                return buf[pos++];
            else
                return super.read();
        }
    }

code example

 try (
            // 创建一个PushbackReader对象,指定推回缓冲区的长度为64
            PushbackReader pr = new PushbackReader(new FileReader(
                    "d://2017.txt"), 64);

    ) {
        char[] buf = new char[32];
        // 用以保存上次读取字符串的内容
        String lastContent = "";
        int hasRead = 0;

        // 循环读取文件内容
        while ((hasRead = pr.read(buf)) > 0) {
            // 将读取的内容转化为字符串
            String content = new String(buf, 0, hasRead);
            int targetIndex = 0;

            // 将上次读取的字符串和本次读取的字符串拼接起来
            // 查看是否包含目标字符串,
            // 如果包含目标字符串
            if ((targetIndex = (lastContent + content)
                    .indexOf("干物妹")) > 0) {
                // 将本次的内容和上次的内容一起推回缓冲区
                pr.unread((lastContent + content).toCharArray());

                // 重现定义一个长度为targetIndex的char类型的数组
                if (targetIndex > 32) {
                    buf = new char[targetIndex];
                }

                // 再次读取指定长度的内容,即目标字符串之前的内容
                pr.read(buf, 0, targetIndex);

                // 答应读取指定长度的内容
                System.out.println(new String(buf, 0, targetIndex));
                System.exit(0);
            } else {

                // 打印上次读取的内容
                System.out.println(lastContent);
                // 将本次读取的内容设置为上次读取的内容
                lastContent = content;

            }

        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

FilterWriter

Overview

write picture description here

Official Notes

The FilterWriter class is an abstract class used to write out the filtered byte stream.
This class provides a default method to pass all requests to the underlying stream of the package. The class that
uses this class as the base class needs to override some methods of this class and may Need to provide additional methods and parameters

Source code analysis

Alt text

The FilterWriter class is similar to FilterReader.
Basically there are no methods of its own. All methods are inherited from Reader.
You need to inherit FilterWriter and implement your own methods before you can use them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647119&siteId=291194637