📅  最后修改于: 2020-09-27 07:22:17             🧑  作者: Mango
Java PushbackInputStream类重写InputStream并为另一个输入流提供额外的功能。它可以读取已读取的字节,然后将其回推一个字节。
让我们看一下java.io.PushbackInputStream类的声明:
public class PushbackInputStream extends FilterInputStream
Method | Description |
---|---|
int available() | It is used to return the number of bytes that can be read from the input stream. |
int read() | It is used to read the next byte of data from the input stream. |
boolean markSupported() | |
void mark(int readlimit) | It is used to mark the current position in the input stream. |
long skip(long x) | It is used to skip over and discard x bytes of data. |
void unread(int b) | It is used to pushes back the byte by copying it to the pushback buffer. |
void unread(byte[] b) | It is used to pushes back the array of byte by copying it to the pushback buffer. |
void reset() | It is used to reset the input stream. |
void close() | It is used to close the input stream. |
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