核心提示:Java IO 流整理 一. Input和Output 1. stream代表的是任何有能力產出數據的數據源,或是任何有能力接收數據的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括兩種類型: 1.1 以字節爲導向的stream 以字節爲導向的stream,表示以字節爲
Java IO 流整理
一. Input和Output
1. stream代表的是任何有能力產出數據的數據源,或是任何有能力接收數據的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括兩種類型:
1.1 以字節爲導向的stream
以字節爲導向的stream,表示以字節爲單位從stream中讀取或往stream中寫入信息。以字節爲導向的stream包括下面幾種類型:
1.1.1) input stream:
1) ByteArrayInputStream:把內存中的一個緩衝區作爲InputStream使用
2) StringBufferInputStream:把一個String對象作爲InputStream
3) FileInputStream:把一個文件作爲InputStream,實現對文件的讀取操作
4) PipedInputStream:實現了pipe的概念,主要在線程中使用
5) SequenceInputStream:把多個InputStream合併爲一個InputStream
1.1.2) Out stream
1) ByteArrayOutputStream:把信息存入內存中的一個緩衝區中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:實現了pipe的概念,主要在線程中使用
4) SequenceOutputStream:把多個OutStream合併爲一個OutStream
1.2 以Unicode字符爲導向的stream
以Unicode字符爲導向的stream,表示以Unicode字符爲單位從stream中讀取或往stream中寫入信息。以Unicode字符爲導向的stream包括下面幾種類型:
1.2.1) Input Stream
1) CharArrayReader:與ByteArrayInputStream對應
2) StringReader:與StringBufferInputStream對應
3) FileReader:與FileInputStream對應
4) PipedReader:與PipedInputStream對應
1.2.2) Out Stream
1) CharArrayWrite:與ByteArrayOutputStream對應
2) StringWrite:無與之對應的以字節爲導向的stream
3) FileWrite:與FileOutputStream對應
4) PipedWrite:與PipedOutputStream對應
以字符爲導向的stream基本上對有與之相對應的以字節爲導向的stream。兩個對應類實現的功能相同,字是在操作時的導向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把內存中的一個緩衝區作爲InputStream使用,所不同的是前者每次從內存中讀取一個字節的信息,而後者每次從內存中讀取一個字符。
1.3 兩種不現導向的stream之間的轉換
InputStreamReader和OutputStreamReader:把一個以字節爲導向的stream轉換成一個以字符爲導向的stream。
2. stream添加屬性
2.1 “爲stream添加屬性”的作用
運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以爲stream添加屬性。下面以一個例子來說明這種功能的作用。
假如我們要往一個文件中寫入數據,我們可以這樣操作:
FileOutStream fs = new FileOutStream(“test.txt”);
然後就可以通過產生的fs對象調用write()函數來往test.txt文件中寫入數據了。但是,假如我們想實現“先把要寫入文件的數據先緩存到內存中,再把緩存中的數據寫入文件中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和FilterOutStream的子類,爲FileOutStream添加我們所需要的功能。
2.2 FilterInputStream的各種類型
2.2.1 用於封裝以字節爲導向的InputStream
1) DataInputStream:從stream中讀取基本類型(int、char等)數據。
2) BufferedInputStream:使用緩衝區
3) LineNumberInputStream:會記錄input stream內的行數,然後可以調用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用於編譯器開發
2.2.2 用於封裝以字符爲導向的InputStream
1) 沒有與DataInputStream對應的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream
2) BufferedReader:與BufferedInputStream對應
3) LineNumberReader:與LineNumberInputStream對應
4) PushBackReader:與PushbackInputStream對應
2.3 FilterOutStream的各種類型
2.2.3 用於封裝以字節爲導向的OutputStream
1) DataIOutStream:往stream中輸出基本類型(int、char等)數據。
2) BufferedOutStream:使用緩衝區
3) PrintStream:產生格式化輸出
2.2.4 用於封裝以字符爲導向的OutputStream
1) BufferedWrite:與對應
2) PrintWrite:與對應
3. RandomAccessFile
2.3) 可通過RandomAccessFile對象完成對文件的讀寫操作
2) 在產生一個對象時,可指明要打開的文件的性質:r,只讀;w,只寫;rw可讀寫
3) 可以直接跳到文件中指定的位置
4. I/O應用的例子
1. import java.io.*;
2. public class TestIO
3. {
4. public static void main(String[] args) throws IOException
5. {
6.
7.
8. view plaincopy to clipboardprint?
9. //1.以行爲單位從一個文件讀取數據
10. BufferedReader in =new BufferedReader(new FileReader("F://nepalon//TestIO.java"));
11. String s, s2 = new String();
12. while((s = in.readLine()) != null)
13. s2 += s + "/n";
14. in.close();
15. //1.以行爲單位從一個文件讀取數據
16. BufferedReader in =new BufferedReader(new FileReader("F://nepalon//TestIO.java"));
17. String s, s2 = new String();
18. while((s = in.readLine()) != null)
19. s2 += s + "/n";
20. in.close();
21. view plaincopy to clipboardprint?
22. ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
23. //1b. 接收鍵盤的輸入
24. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
25. System.out.println("Enter a line:");
26. System.out.println(stdin.readLine());
27. //1b. 接收鍵盤的輸入
28. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
29. System.out.println("Enter a line:");
30. System.out.println(stdin.readLine());
31.
32. view plaincopy to clipboardprint?
33. //2. 從一個String對象中讀取數據
34. StringReader in2 = new StringReader(s2);
35. int c;
36. while((c = in2.read()) != -1)
37. System.out.println((char)c);
38. in2.close();
39. //2. 從一個String對象中讀取數據
40. StringReader in2 = new StringReader(s2);
41. int c;
42. while((c = in2.read()) != -1)
43. System.out.println((char)c);
44. in2.close();
45.
46.
47. view plaincopy to clipboardprint?
48. //3. 從內存取出格式化輸入
49. try
50. {
51. DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
52. while(true)
53. System.out.println((char)in3.readByte());
54. }
55. catch(EOFException e)
56. {
57. System.out.println("End of stream");
58. }
59. //3. 從內存取出格式化輸入
60. try
61. {
62. DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
63. while(true)
64. System.out.println((char)in3.readByte());
65. }
66. catch(EOFException e)
67. {
68. System.out.println("End of stream");
69. }
70.
71. view plaincopy to clipboardprint?
72. //4. 輸出到文件
73. try
74. {
75. BufferedReader in4 = new BufferedReader(new StringReader(s2));
76. PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("F://nepalon// TestIO.out")));
77. int lineCount = 1;
78. while((s = in4.readLine()) != null)
79. out1.println(lineCount++ + ":" + s);
80. out1.close();
81. in4.close();
82. }
83. catch(EOFException ex)
84. {
85. System.out.println("End of stream");
86. }
87. //4. 輸出到文件
88. try
89. {
90. BufferedReader in4 = new BufferedReader(new StringReader(s2));
91. PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("F://nepalon// TestIO.out")));
92. int lineCount = 1;
93. while((s = in4.readLine()) != null)
94. out1.println(lineCount++ + ":" + s);
95. out1.close();
96. in4.close();
97. }
98. catch(EOFException ex)
99. {
100. System.out.println("End of stream");
101. }
102.
103.view plaincopy to clipboardprint?
104.//5. 數據的存儲和恢復
105.try
106.{
107. DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F://nepalon// Data.txt")));
108. out2.writeDouble(3.1415926);
109. out2.writeChars("/nThas was pi:writeChars/n");
110. out2.writeBytes("Thas was pi:writeByte/n");
111. out2.close();
112. DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("F://nepalon// Data.txt")));
113. BufferedReader in5br =new BufferedReader(new InputStreamReader(in5));
114. System.out.println(in5.readDouble());
115. System.out.println(in5br.readLine());
116. System.out.println(in5br.readLine());
117.}
118.catch(EOFException e)
119.{
120. System.out.println("End of stream");
121.}
122. //5. 數據的存儲和恢復
123. try
124. {
125. DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F://nepalon// Data.txt")));
126. out2.writeDouble(3.1415926);
127. out2.writeChars("/nThas was pi:writeChars/n");
128. out2.writeBytes("Thas was pi:writeByte/n");
129. out2.close();
130. DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("F://nepalon// Data.txt")));
131. BufferedReader in5br =new BufferedReader(new InputStreamReader(in5));
132. System.out.println(in5.readDouble());
133. System.out.println(in5br.readLine());
134. System.out.println(in5br.readLine());
135. }
136. catch(EOFException e)
137. {
138. System.out.println("End of stream");
139. }
140.
141.view plaincopy to clipboardprint?
142.//6. 通過RandomAccessFile操作文件
143. RandomAccessFile rf = new RandomAccessFile("F://nepalon//rtest.dat", "rw");
144.for(int i=0; i<10; i++)
145. rf.writeDouble(i*1.414);
146.rf.close();
147.rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
148.for(int i=0; i<10; i++)
149. System.out.println("Value " + i + ":" + rf.readDouble());
150.rf.close();
151.
152.rf = new RandomAccessFile("F://nepalon// rtest.dat", "rw");
153.rf.seek(5*8);
154.rf.writeDouble(47.0001);
155.rf.close();
156.
157.rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
158.for(int i=0; i<10; i++)
159. System.out.println("Value " + i + ":" + rf.readDouble());
160.rf.close();
161. //6. 通過RandomAccessFile操作文件
162. RandomAccessFile rf = new RandomAccessFile("F://nepalon//rtest.dat", "rw");
163. for(int i=0; i<10; i++)
164. rf.writeDouble(i*1.414);
165. rf.close();
166. rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
167. for(int i=0; i<10; i++)
168. System.out.println("Value " + i + ":" + rf.readDouble());
169. rf.close();
170.
171. rf = new RandomAccessFile("F://nepalon// rtest.dat", "rw");
172. rf.seek(5*8);
173. rf.writeDouble(47.0001);
174. rf.close();
175.
176. rf = new RandomAccessFile("F://nepalon// rtest.dat", "r");
177. for(int i=0; i<10; i++)
178. System.out.println("Value " + i + ":" + rf.readDouble());
179. rf.close();
180.
181. }
}