package cn.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件字节输入流FileInputStream
* 1.创建源
* 2.选择流
* 3.操作(分段读取)
* 4.释放资源(try with resource)
*
* @author Chill Lyn
*
*/
public class TestFileInputStream2 {
public static void main(String[] args) {
// 创建源
File src = new File("abc.txt");
// 选择流
try (InputStream is = new BufferedInputStream(new FileInputStream(src))) {
// 操作(分段读取)
byte[] flush = new byte[1024];// 缓冲容器
int len = -1;// 接收长度
while ((len = is.read(flush)) != -1) {
// 字节数组解码成字符串
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java_IO_文件字节输入流FileInputStream

最新推荐文章于 2025-03-04 07:52:57 发布
