
课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
昆明达内培训的老师给大家分享学习java的重点之一:InputStream 字节输入流的使用
(1)FileInputstream:子类,读取数据的通道
使用步骤:
1.获取目标文件:new File()
2.建立通道:new FileInputString()
3.读取数据:read()
4.释放资源:close()
(2)读取数据的三种方式
1.直接读取(一次只能一个字节)
int date = fileInputStream.read();
char date3 = (char)fileInputStream.read();
2.单独使用for循环(效率低)
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
3.Byte[ ]缓冲区(只能读取指定的字节数不能读取一个完整的文件)
byte[] bt = new byte[1024];
int count = fileInputStream.read(bt);
System.out.println(new String (bt,0,count));
4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[] bt = new byte[1024];
int count = 0;
while((count = fileInputStream.read(bt)) != -1){
System.out.println(new String (bt,0,count));
}