Java 缓冲输入流
2018-02-06 19:21 更新
Java IO教程 - Java缓冲输入流
BufferedInputStream通过缓冲数据向输入流添加功能。
它维护一个内部缓冲区以存储从底层输入流读取的字节。
我们创建缓冲区输入流如下:
String srcFile =“test.txt";BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
以下代码显示如何使用BufferedInputStream从文件读取。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) {
String srcFile = "test.txt";
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile))) {
// Read one byte at a time and display it
byte byteData;
while ((byteData = (byte) bis.read()) != -1) {
System.out.print((char) byteData);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
上面的代码生成以下结果。

以上内容是否对您有帮助:

免费 AI IDE


更多建议: