Java PushbackInputStream
Java PushbackInputStream类覆盖InputStream并为另一个输入提供额外的功能流。它可以读取已经读取的一个字节,然后将其回推一个字节。
类声明
让我们看一下java.io.PushbackInputStream类的声明:
public class PushbackInputStream extends FilterInputStream
类方法
方法 |
说明 |
int available() |
它用于返回可从输入流读取的字节数。 |
int read() |
它用于从输入流中读取下一个数据字节。 |
boolean markSupported() |
用于测试输入流是否支持mark和reset方法。 |
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);
}
}
}
}
输出: