FileInputStream 缺点
调用 FileInputStream 的 read() 方法,立即读取一个字节,这样效率低下
解决
使用 BufferedInputStream,调用 read() 方法,默认读取 8k 的数据,放入到缓存中,然后从返回缓存的第一个字节
再次调用 read() 方法,直接从返回缓存的第二个字节
优点:效率高,避免频繁读硬盘
代码
package stdfile;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StdBuffInput {
public static void main(String[] args) {
FileInputStream fis=null;
BufferedInputStream bis=null;
try {
fis=new FileInputStream("D:\\1测试.txt");
bis = new BufferedInputStream(fis);
int i=bis.read();//调用 read(),默认读取 8k的数据,然后返回第一个字节
System.out.println( (char) i );
i=bis.read();//将缓存中的第二个字节返回
System.out.println( (char) i );
System.out.println();
}catch(Exception e){
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

