核心提示: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. }
}