緩沖流 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 args
12 * @throws IOException
13 */
14 public static void main(String[] args) throws IOException {
15 // TODO Auto-generated method stub
16 //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 }