摘要:
下文使用Java代码中PushbackInputStream类的用法分享,如下所示:
PushbackInputStream类简介:
PushbackInputStream的功能为添加到另一个输入流以推回或读取一个字节
PushbackInputStream类的read()方法从输入流中读取一个字节
unread()方法将一个字节推回输入流
例:
Java 中PushbackInputStream类的示例
package com.maomao365;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
/**
* @author maomao365
*/
public class PushbackInputStreamExample {
public static void main(String[] args) {
String data = "这里是maomao365.com的PushbackInputStream类用法";
ByteArrayInputStream byteArrayInputStream = null;
PushbackInputStream pushbackInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(data.getBytes());
pushbackInputStream = new PushbackInputStream(byteArrayInputStream);
//读取第一个字符到流中
int i=pushbackInputStream.read();
System.out.println((char)i);
//弹出刘中的字符
pushbackInputStream.unread(i);
//输出现在流中的字符
byte b[] = new byte[data.getBytes().length];
pushbackInputStream.read(b);
System.out.println(new String(b));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pushbackInputStream != null) {
pushbackInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}