Java-IO之PushbackInputStream
来源:互联网 发布:电火花电脉冲编程 编辑:程序博客网 时间:2024/05/21 17:48
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'); } } }}
0 0
- Java-IO之PushbackInputStream
- Java IO之PushbackInputStream类
- 探究java IO之PushbackInputStream类
- Java IO--回退流PushbackInputStream
- Java IO - PushbackInputStream&PushbackReader
- java IO笔记(PushbackInputStream)
- Java IO系列4 字节流之PushbackInputStream
- 18.Java IO: FilterInputStream 、FilterOutputStream、PushbackInputStream
- Java IO操作——回退流PushbackInputStream
- IO-PushBackInputStream、ObjectInputStream、ObjectOutputStream
- javaI/O之PushbackInputStream
- javaI/O之PushbackInputStream
- pushbackInputstream
- pushbackInputStream
- PushbackInputStream
- PushbackInputStream
- PushbackInputStream
- PushbackInputStream
- 《卓有成效的管理者》第一次心得
- [Setting]VS2013安装失败-无法建立到信任根颁发机构的证书链
- js里面关于数字 字符串 数组 的问题
- Codeforces 580D Kefa and Dishes
- linux 安装mysql
- Java-IO之PushbackInputStream
- Android对SQLite的"增删改查"——SQLiteDatabase
- POJ.1185 炮兵阵地
- HDU 2062 Subset sequence[排列组合]
- HttpModule实现URL重写
- Java中的继承和多态
- MSHFlexGrid控件——点击某行时该行被选中
- nginx监听端口非80时的转发问题的配置文件
- 【poj1655】Balancing Act