bufferedinputstream是fileInputstream的子类,它继承自fileInputstream,所以它是在fileInputstream上的封装,自然而然效率肯定比fileInputstream好。
我们通过代码分析,来看为什么bufferedinputstream比fileInputstream效率高
File file=new File("D:/text.txt");
FileInputStream fileInputStream;
byte b[]=new byte[10];
int length;
try {
fileInputStream = new FileInputStream(file);
while((length=fileInputStream.read(b))!=-1){
String str= new String(b, 0, length);
//do something 一系列业务处理
}
} catch (Exception e1) {
}
上面代码是从fileInputStream中读取10个单位的字节放在b[]字节数组中,然后对这10个字节进行业务处理,注意这时候进行业务处理时fileInputStream.read(b)是阻塞的,即输入流处于阻塞状态,需等业务处理之后才再次执行while循环里的fileInputSt