代码如下:
package com.zhongjing.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class FileInputStreamDemo {
/**
* FileInputStream 字节输入流 --> 读取数据
* @param args
*/
public static void main(String[] args) {
FileInputStream fis = null;
File file = new File("D:/test.txt");
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024]; //数据中转站 临时缓冲区
int length = 0;
//循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
//当文件读取到结尾时返回 -1,循环结束。
while((length = fis.read(buf)) != -1){
System.out.println(new String(buf, 0, length));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();//强制关闭输入流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果如下:
标签: 使用 文件 内容 FileInputStream
版权声明:本站所有图片/内容除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!