Java教程-Java PushbackInputStream类

Java PushbackInputStream类
Java PushbackInputStream类覆盖了InputStream类并为另一个输入流提供了额外的功能。它可以取消读取的字节并将一个字节推回。
类声明
让我们看一下java.io.PushbackInputStream类的声明:
public class PushbackInputStream extends FilterInputStream
类方法
它用于测试输入流是否支持mark和reset方法。
Method | Description |
---|---|
int available() | 它用于返回可以从输入流中读取的字节数。 |
int read() | 它用于从输入流中读取下一个字节的数据。 |
boolean markSupported() | |
void mark(int readlimit) | 它用于标记输入流中的当前位置。 |
long skip(long x) | 它用于跳过并丢弃 x 字节的数据。 |
void unread(int b) | 它用于通过将字节复制到推回缓冲区来推回字节。 |
void unread(byte[] b) | 它用于通过将字节数组复制到推回缓冲区来推回字节数组。 |
void reset() | 它用于重置输入流。 |
void close() | 它用于关闭输入流。 |
PushbackInputStream类的示例
import java.io.*;
public class InputStreamExample {
public static void main(String[] args)throws Exception{
String srg = "1##2#34###12";
byte ary[] = srg.getBytes();
ByteArrayInputStream array = new ByteArrayInputStream(ary);
PushbackInputStream push = new PushbackInputStream(array);
int i;
while( (i = push.read())!= -1) {
if(i == '#') {
int j;
if( (j = push.read()) == '#'){
System.out.print("**");
}else {
push.unread(j);
System.out.print((char)i);
}
}else {
System.out.print((char)i);
}
}
}
}
输出:
1**2#34**#12