緩衝流是處理流的一種html
處理流就是「套接」在已有的流的基礎上java
字節:數組
字符:code
緩衝流的主要做用是提升文件的讀寫效率視頻
字節型htm
實例化File類的對象,指明要操做的文件對象
提供具體的流blog
2.1 造節點流get
2.2 造緩衝流input
數據的寫出輸入過程
流的關閉操做
try-catch-finally異常處理
FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //1. 實例化File類的對象,指明要操做的文件 File srcFile = new File("C:\\Users\\LENOVO\\Desktop\\丸子.png"); File destFile = new File("C:\\Users\\LENOVO\\Desktop\\殷志源.png"); //2. 提供具體的流 //2.1 造節點流 fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //2.2 造緩衝流 bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //3. 讀取和寫入過程 byte[] buffer = new byte[10]; int len; while((len = bis.read(buffer)) != -1 ){ bos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4. 關閉 try { if(bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } //關閉內層流省略 // try { // if(fos != null) // fos.close(); // } catch (IOException e) { // e.printStackTrace(); // } // // try { // if(fis != null) // fis.close(); // } catch (IOException e) { // e.printStackTrace(); // } }
兩次代碼的byte[]數組設定的長度相同,都是byte[1024],複製的都是同一個視頻
利用節點流花費的時間爲339(在節點流章節2.2中能夠查看)
http://www.javashuo.com/article/p-ndkwhcha-mx.html
利用緩衝流花費的時間爲77
其內部提供了一個緩衝區
文件在讀取的時候先將文件讀取到緩衝區,達到緩衝區指定大小後一次性的寫出
BufferedOutputStream中有一個方法
bos.flush; //刷新緩衝區(會自動刷新,不用顯式調用)
超出緩衝區指定的大小的時候,會自動的刷新出去(把緩衝區的數據狀況,將數據寫出去)
和BufferedInputStream和BufferedOutputStream用法類似
BufferedInputStream和BufferedOutputStream「包」的是FileInputStream和FileOutputStream
BufferedReader和BufferedWriter「包」的是FileReader和FileWriter
具體緣由見下圖( http://www.javashuo.com/article/p-ewtsqfat-mx.html )
讀操做的步驟中的兩種寫法
char[] cbuf = new char[1024]; int len; while((len = bfr.read(cbuf)) != -1){ bfw.write(cbuf,0,len); }
String data; while((data = bfr.readLine()) != null){ bfw.write(data + "\n"); }
bfw.write(data );
bfw.write(data); bfw.newLine();
文本文件的複製
public void copytxt(String srcPath, String destPath){ BufferedReader bfr = null; BufferedWriter bfw = null; try { File srcFile = new File(srcPath); File destFile = new File(destPath); bfr = new BufferedReader(new FileReader(srcFile)); bfw = new BufferedWriter(new FileWriter(destFile)); //方式一 // char[] cbuf = new char[1024]; // int len; // while((len = bfr.read(cbuf)) != -1){ // bfw.write(cbuf,0,len); // } //方式二 String data; while((data = bfr.readLine()) != null){ //2.1 // bfw.write(data + "\n");//data中不包含換行符 //2.2 bfw.write(data); bfw.newLine(); } System.out.println("複製成功"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bfr != null) bfr.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bfw != null) bfw.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test6(){ String srcPath = "C:\\Users\\LENOVO\\Desktop\\hello.txt"; String destPath = "C:\\Users\\LENOVO\\Desktop\\hello(1).txt"; long start = System.currentTimeMillis(); copytxt(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println("花費的時間:" + (end - start)); }