public class Main1 {
/**
* @param args
*/
public static void main(String[] args) {
// 1,设置路径
String path = "E://0.txt";
// 2,获得File对象
File file = new File(path);
try {
// 3,获得FileInputStream
FileInputStream fileInputStream = new FileInputStream(file);
// 4,获得BufferInputSream对象
BufferedInputStream m_BufferedInputStream = new BufferedInputStream(
fileInputStream);
// 用于保存每次读取的信息
byte[] buffer = new byte[24];
// 6,每次读取的信息放到buffer,如果后面还有数据,read返回的值不等于-1,那么就继续读
while (m_BufferedInputStream.read(buffer) != -1) {
String infor = new String(buffer);
System.out.println(infor);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文写了文件的读取,BufferedInputStream会预先读取一段原始数据进入缓冲区,第一次为8192字节,以后会成倍的增加,在读取文件的时候不需要关闭,但是在写文件的时候文件的写入和关闭必须对应,也就是说,如果是写是缓存,那么关闭也必须是缓存