標籤:子類 auto fileread rate 顯示 span 記憶體 static log
緩衝流 Buffer :設定緩衝區加快執行效率
子類:
(一)BufferedInputStream : 緩衝輸入位元組流 ,目的:提高讀取檔案的效率
注意: BufferedInputStream 他是沒有讀寫資料的功能
內部實現 : 你面維護了一個8位元組的byte數組。
使用步驟:
1.找到一個目標檔案.
2.建立通道 new FileInputStream(" ");
3.建立一個緩衝位元組輸入資料流 new BufferedInputStream(FileInputStream);
4.讀取資料 read();
5.關閉資源 close();
(二)BufferedOutputStream :緩衝輸出位元組流 內部維護了一個 8k的位元組數組
作用: 提高檔案的輸出的效率,可以提供其他的方法。
使用:
1.找目標
2.建立通道 FileOutputStream
3.建立一個緩衝位元組輸出資料流
4.寫資料,不會寫入到磁碟中。 如果數組中的資料已經滿了,才會自動將資料寫入到磁碟中。
5.將資料寫入到磁碟 : 調用flush(),或者關閉資源。
6.關閉資源。
(三)BuffredRead 緩衝輸入字元流。
有一個擴充的功能:readLine(); // 可以一次讀一行文字。
(四)BufferedWriter: 緩衝輸出字元流
內部提供一個8192長度的字元數組作為這樣一個緩衝區。
BufferedWriter作用 :提高寫入的效率,拓展FileWriter的功能。
newLine(); //換行寫入資料
簡單的位元組緩衝流案例
1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class buffered { 9 10 /**11 * @param args12 * @throws IOException 13 */14 public static void main(String[] args) throws IOException {15 // TODO Auto-generated method stub16 //bufInTest();17 bufOutTest();18 }19 20 //(1)BufferedInputStream 的使用21 public static void bufInTest() throws IOException{22 //1.找到目標23 File file = new File("D:\\a.txt");24 //2.建立通道25 FileInputStream fis = new FileInputStream(file);26 27 //**3.建立一個緩衝輸入位元組流******28 BufferedInputStream bfis = new BufferedInputStream(fis);29 30 //4.開始寫入資料31 int content = 0; // 一次只會取一個位元組32 while ((content= bfis.read())!= -1) { // 還是一個一個的讀取 問什麼可以提高效率呢?33 System.out.print((char)content);34 }35 //5.關閉通道36 bfis.close();37 }38 39 //(2)BufferedOutputStream 的使用40 public static void bufOutTest() throws IOException{41 42 //1.找目標43 File file = new File("D:\\b.txt");44 //2.建立通道45 FileOutputStream fos = new FileOutputStream(file);46 47 //3.建立緩衝流48 BufferedOutputStream bfos = new BufferedOutputStream(fos);49 50 //4.建立寫入檔案的資料51 String string = "good good study day day up";52 53 //5.寫資料, 到這一步只是將資料儲存到記憶體中位元組數組中。54 bfos.write(string.getBytes()); 55 56 //6.再重新整理 將資料寫入到磁碟中57 //bfos.flush();58 59 //7.關閉資源60 bfos.close();//內部會實現flush();61 }62 }
簡單的字元緩衝流案例
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileReader; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 8 public class fileReader { 9 10 public static void main(String[] args) throws IOException {11 testBufferedWriter();12 testBufferedRead();13 }14 //(1)緩衝輸出字元流的使用15 public static void testBufferedWriter() throws IOException{16 17 //1.建立一個通道,指定一個路徑18 FileWriter fiw = new FileWriter("D:\\a.txt",true);19 20 //2.建立緩衝流21 BufferedWriter bfw = new BufferedWriter(fiw);22 23 //讓資料換行顯示24 bfw.newLine(); //換行寫入資料25 26 //3.開始寫入資料27 bfw.write("上課不要吃東西");28 29 //4.關閉資源30 bfw.close(); // 關閉資源之前會做一個重新整理操作。調用flush()方法。 31 }32 33 //(2)緩衝輸入字元流的使用34 public static void testBufferedRead() throws IOException{35 36 //1.建立通道並且指定路徑37 FileReader fir = new FileReader("D:\\b.txt");38 39 //2.建立緩衝流40 BufferedReader bfr = new BufferedReader(fir);41 42 //3.1、開始讀取資料(一次讀取一個位元組)43 int content = 0;44 while ((content = bfr.read()) != -1) {45 46 System.out.print((char)content);47 }48 49 //3.2、readLine()擴充功能,讀取一行資料 50 String string1 = bfr.readLine();51 System.out.println(string1);52 53 //一次讀取一行資料(返回字串),效率更高54 String string = null;55 while ((string = bfr.readLine()) != null) {56 System.out.println(string);57 }58 59 //4.關閉60 bfr.close();61 }62
java 緩衝流 Buffer