--- 缓冲流 (默认缓冲区8KB 1024*8)
bufferedInputStream、bufferedOutputStream、
//字节缓冲输出流 第二个参数true代表 向文件追加而不是覆盖
OutputStream out = new FileOutputStream(new File("1.txt"),true);
BufferedOutputStream bos = new BufferedOutputStream(out);
bos.write("hello".getBytes());
bos.close();
//字节缓冲输入流
InputStream is = new FileInputStream(new File("1.txt"));
BufferedInputStream bis = new BufferedInputStream(is);
//读取方法和基本流是一样的,因为缓冲流只是把8KB的数据读取到内存中
//实际取得时候从内存中获取,速度会更快从而达到每8KB读取一次磁盘的效果
byte[] b= new byte[1024];
int i = -1;
while((i=bis.read(b))!=-1){
System.out.println(new String(b));
}
bis.close();
}
bufferedReader、bufferedWrite
public static void main(String[] args) throws Exception{Writer w = new FileWriter("1.txt");
BufferedWriter bw = new BufferedWriter(w);
bw.write("考虑房价开始了多久弗兰克斯剪短发了");
//换行
bw.newLine();
bw.write("\n");
bw.write("123345");
bw.close();
Reader rd = new FileReader("1.txt");
BufferedReader br = new BufferedReader(rd);
//之前的方法
/*char[] ch = new char[1024];
int j = -1;
while((j=br.read(ch))!=-1){
System.out.println(new String(ch,0,j));
}*/
//BufferedReader自己的方法
String str = null;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
}
--- 测试缓冲流和普通流的速度
public static void main(String[] args) throws Exception{
/*System.out.println(System.currentTimeMillis());//4954
File f = new File("Wildlife.wmv");
File f1 = new File("x1.wmv");
InputStream is = new FileInputStream(f);
OutputStream os = new FileOutputStream(f1);
byte[] by = new byte[1024];
int i = -1;
while((i=is.read(by))!=-1){
os.write(by, 0, i);
}
is.close();
os.close();
System.out.println(System.currentTimeMillis());//5080
*/
System.out.println(System.currentTimeMillis());//7849
File f2 = new File("Wildlife.wmv");
File f3 = new File("x2.wmv");
InputStream is1 = new FileInputStream(f2);
OutputStream os1 = new FileOutputStream(f3);
BufferedInputStream bis = new BufferedInputStream(is1);
BufferedOutputStream bos = new BufferedOutputStream(os1);
byte[] by1 = new byte[1024];
int j = -1;
while((j=bis.read(by1))!=-1){
bos.write(by1, 0,j);
}
bis.close();
bos.close();
System.out.println(System.currentTimeMillis());//7908
}