BufferedInputStream
BufferedInputStream 是緩沖輸入流。它繼承于FilterInputStream。
BufferedInputStream 的作用是為另一個輸入流添加一些功能,例如,提供“緩沖功能”以及支持“mark()標記”和“reset()重置方法”。
BufferedInputStream 本質上是通過一個內部緩沖區數組實現的。例如,在新建某輸入流對應的BufferedInputStream后,當我們通過read()讀取輸入流的數據時,BufferedInputStream會將該輸入流的數據分批的填入到緩沖區中。每當緩沖區中的數據被讀完之后,輸入流會再次填充數據緩沖區;如此反復,直到我們讀完輸入流數據位置。
BufferedInputStream 函數列表:
1
2
3
4
5
6
7
8
9
10
11
|
BufferedInputStream(InputStream in) BufferedInputStream(InputStream in, int size) synchronized int available() void close() synchronized void mark(int readlimit) boolean markSupported() synchronized int read() synchronized int read(byte[] buffer, int offset, int byteCount) synchronized void reset() synchronized long skip(long byteCount) |
示例代碼:
關于BufferedInputStream中API的詳細用法,參考示例代碼(BufferedInputStreamTest.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.lang.SecurityException; /** * BufferedInputStream 測試程序 * * @author skywang */ public class BufferedInputStreamTest { private static final int LEN = 5 ; public static void main(String[] args) { testBufferedInputStream() ; } /** * BufferedInputStream的API測試函數 */ private static void testBufferedInputStream() { // 創建BufferedInputStream字節流,內容是ArrayLetters數組 try { File file = new File( "bufferedinputstream.txt" ); InputStream in = new BufferedInputStream( new FileInputStream(file), 512 ); // 從字節流中讀取5個字節。“abcde”,a對應0x61,b對應0x62,依次類推... for ( int i= 0 ; i<LEN; i++) { // 若能繼續讀取下一個字節,則讀取下一個字節 if (in.available() >= 0 ) { // 讀取“字節流的下一個字節” int tmp = in.read(); System.out.printf( "%d : 0x%s\n" , i, Integer.toHexString(tmp)); } } // 若“該字節流”不支持標記功能,則直接退出 if (!in.markSupported()) { System.out.println( "make not supported!" ); return ; } // 標記“當前索引位置”,即標記第6個位置的元素--“f” // 1024對應marklimit in.mark( 1024 ); // 跳過22個字節。 in.skip( 22 ); // 讀取5個字節 byte [] buf = new byte [LEN]; in.read(buf, 0 , LEN); // 將buf轉換為String字符串。 String str1 = new String(buf); System.out.printf( "str1=%s\n" , str1); // 重置“輸入流的索引”為mark()所標記的位置,即重置到“f”處。 in.reset(); // 從“重置后的字節流”中讀取5個字節到buf中。即讀取“fghij” in.read(buf, 0 , LEN); // 將buf轉換為String字符串。 String str2 = new String(buf); System.out.printf( "str2=%s\n" , str2); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
程序中讀取的bufferedinputstream.txt的內容如下:
1
2
3
|
abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ |
運行結果:
1
2
3
4
5
6
7
|
0 : 0x61 1 : 0x62 2 : 0x63 3 : 0x64 4 : 0x65 str1=01234 str2=fghij |
BufferedOutputStream
BufferedOutputStream 是緩沖輸出流。它繼承于FilterOutputStream。
BufferedOutputStream 的作用是為另一個輸出流提供“緩沖功能”。
BufferedOutputStream 函數列表:
1
2
3
4
5
6
7
|
BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int size) synchronized void close() synchronized void flush() synchronized void write(byte[] buffer, int offset, int length) synchronized void write(int oneByte) |
示例代碼:
關于BufferedOutputStream中API的詳細用法,參考示例代碼(BufferedOutputStreamTest.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import java.io.BufferedOutputStream; import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Scanner; /** * BufferedOutputStream 測試程序 * * @author skywang */ public class BufferedOutputStreamTest { private static final int LEN = 5 ; // 對應英文字母“abcddefghijklmnopqrsttuvwxyz” private static final byte [] ArrayLetters = { 0x61 , 0x62 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0x68 , 0x69 , 0x6A , 0x6B , 0x6C , 0x6D , 0x6E , 0x6F , 0x70 , 0x71 , 0x72 , 0x73 , 0x74 , 0x75 , 0x76 , 0x77 , 0x78 , 0x79 , 0x7A }; public static void main(String[] args) { testBufferedOutputStream() ; } /** * BufferedOutputStream的API測試函數 */ private static void testBufferedOutputStream() { // 創建“文件輸出流”對應的BufferedOutputStream // 它對應緩沖區的大小是16,即緩沖區的數據>=16時,會自動將緩沖區的內容寫入到輸出流。 try { File file = new File( "out.txt" ); OutputStream out = new BufferedOutputStream( new FileOutputStream(file), 16 ); // 將ArrayLetters數組的前10個字節寫入到輸出流中 out.write(ArrayLetters, 0 , 10 ); // 將“換行符\n”寫入到輸出流中 out.write( '\n' ); // TODO! //out.flush(); readUserInput() ; out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 讀取用戶輸入 */ private static void readUserInput() { System.out.println( "please input a text:" ); Scanner reader= new Scanner(System.in); // 等待一個輸入 String str = reader.next(); System.out.printf( "the input is : %s\n" , str); } } |
運行結果:
生成文件“out.txt”,文件的內容是“abcdefghij”。
分步測試: 分別按照下面3種步驟測試程序,來查看緩沖區大小以及flush()的作用。
第1種:原始程序
(1) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為空。
(2) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)的結果不同;之所以(01)中的out.txt內容為空,是因為out.txt對應的緩沖區大小是16字節,而我們只寫入了11個字節,所以,它不會執行清空緩沖操作(即,將緩沖數據寫入到輸出流中)。
而(02)對應out.txt的內容是“abcdefghij”,是因為執行了out.close(),它會關閉輸出流;在關閉輸出流之前,會將緩沖區的數據寫入到輸出流中。
注意:重新測試時,要先刪除out.txt。
第2種:在readUserInput()前添加如下語句
out.flush();
這句話的作用,是將“緩沖區的內容”寫入到輸出流中。
(1) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
(2) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)結果一樣,對應out.txt的內容都是“abcdefghij”。這是因為執行了flush()操作,它的作用是將緩沖區的數據寫入到輸出流中。
注意:重新測試時,要先刪除out.txt!
第3種:在第1種的基礎上,將
1
|
out.write(ArrayLetters, 0 , 10 ); |
修改為
1
|
out.write(ArrayLetters, 0 , 20 ); |
(1) 運行程序。在程序等待用戶輸入時,查看“out.txt”的文本內容;發現:內容為“abcdefghijklmnopqrst”(不含回車)。
(02) 運行程序。在用戶輸入之后,查看“out.txt”的文本內容;發現:內容為“abcdefghijklmnopqrst”(含回車)。
從中,我們發現(01)運行結果是“abcdefghijklmnopqrst”(不含回車)。這是因為,緩沖區的大小是16,而我們通過out.write(ArrayLetters, 0, 20)寫入了20個字節,超過了緩沖區的大小;這時,會直接將全部的輸入都寫入都輸出流中,而不經過緩沖區。
(3)運行結果是“abcdefghijklmnopqrst”(含回車),這是因為執行out.close()時,將回車符號'\n'寫入了輸出流中。
注意:重新測試時,要先刪除out.txt!