1、功能:以字节的方式读取文件的内容
2、常用方法:
int read():以字节的方式读取文件的内容 并反馈字符所代表的整数[0,255],超过范围会失真。规定:当反馈一个-1时表示结束
int close():关闭字节输入流 释放所占资源 确保安全
3、读取文件的步骤:
String path = "e://a.txt";
FileInputStream fis=null;
try {
fis = new FileInputStream(path);
Byte[] bt = new Byte[2048];//一次读取2048字节(缓冲区思想)
int x = fis.read(bt);//获取字节流的范围是[0,255]
while(x>=0){
System.out.print((char)x);
x = fis.read(bt);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件路径不正确");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件输出字节流失败");
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、解决失真问题:
1)用FileReader解决