Java-IO之PushbackInputStream

1、当程序调用推回输入流的unread()方法时,系统会把指定数组的内容推回到该缓冲区中,而推回输入流每次调用read()方法时,总是先从推回缓冲区读取内容,只有完全读取了推回缓冲区里的内容后,但是还没有装满read()所需要的数组时才会从原输入流中读取

example

下面的程序demo5()试图找出程序中的”new PushbackReader”字符串,找到后然后打印出目标字符串之前的内容

import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.io.PushbackReader;

public class PushbackIO {
    public static void main(String[] args) throws Exception {
        // demo1();
        // demo2();
        // demo3();
        // demo4();
        demo5();
    }

    private static void demo5() {
        try (
        // 创建一个PushbackReader对象,指定推回缓冲区的长度为64
        PushbackReader pr = new PushbackReader(new FileReader("D:\\a.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("new PushbackReader")) > 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);
                    System.out.println("------------------------------------");
                    char[] c = new char[32];
                    int len = 0;
                    while((len = pr.read(c))>0){
                        System.out.println(String.valueOf(c, 0, len));
                    }
                } else {

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

                }

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

    }

    private static void demo4() throws Exception {
        PushbackInputStream in = new PushbackInputStream(System.in);
        in.unread('a');
        int ch = in.read();
        System.out.println((char) ch);
        in.close();

        PushbackReader reader = new PushbackReader(new InputStreamReader(
                System.in));
        reader.unread('b');
        ch = reader.read();
        System.out.println((char) ch);
        reader.close();
    }

    private static void demo3() throws Exception {
        String s = "abcdefg";
        /*
         * PushbackInputStream pbin = new PushbackInputStream(in,4)
         * 这个构造函数创建的对象一次可以回推一个缓存
         */
        try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
                PushbackInputStream pbin = new PushbackInputStream(in, 4)) {
            int n;
            byte[] buffer = new byte[4];
            while ((n = pbin.read(buffer)) != -1) {
                System.out.println(new String(buffer));
                // 取回推缓存中的一部分数据
                if (new String(buffer).equals("abcd"))
                    pbin.unread(buffer, 2, 2);
                buffer = new byte[4];
            }
        }

    }

    private static void demo2() throws Exception {
        String s = "abcdefg";
        /*
         * PushbackInputStream pbin = new PushbackInputStream(in,3)
         * 这个构造函数创建的对象一次可以回推一个缓存
         */
        try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
                PushbackInputStream pbin = new PushbackInputStream(in, 3)) {
            int n;
            byte[] buffer = new byte[3];
            while ((n = pbin.read(buffer)) != -1) {
                System.out.println(new String(buffer));
                if (new String(buffer).equals("abc"))
                    pbin.unread(new byte[] { 'M', 'N', 'O' });
                buffer = new byte[3];
            }
        }

    }

    private static void demo1() throws Exception {
        String s = "abcdefg";
        /*
         * PushbackInputStream pbin = new PushbackInputStream(in)
         * 这个构造函数创建的对象一次只能回推一个字节
         */
        try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
                PushbackInputStream pbin = new PushbackInputStream(in)) {
            int n;
            int i = 0;
            while ((n = pbin.read()) != -1) {
                System.out.println(++i + " : " + (char) n);
                if ('b' == n)
                    pbin.unread('U');
            }
        }
    }

}

你可能感兴趣的:(Java,io)