<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h1 {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; mso-outline-level:1; font-size:24.0pt; font-family:宋体; mso-bidi-font-family:宋体; font-weight:bold;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} pre {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} span.attribute {mso-style-name:attribute;} span.attribute-value {mso-style-name:attribute-value;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:278534398; mso-list-template-ids:-220673310;} @list l0:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l1 {mso-list-id:293946490; mso-list-template-ids:1759644904;} @list l1:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l2 {mso-list-id:344866599; mso-list-template-ids:2046099366;} @list l2:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l3 {mso-list-id:382294321; mso-list-template-ids:1628205568;} @list l3:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l4 {mso-list-id:1596861615; mso-list-template-ids:-387699006;} @list l4:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} @list l5 {mso-list-id:1876429222; mso-list-template-ids:-865728192;} @list l5:level1 {mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
Java 输入数据流详解
Java 输入数据流
在Java 中,我们把能够读取一个字节序列的对象称作一个Java 输入数据流; 而我们把够写一个字节序列称作一个输出流。它们分别由抽象类 InputStream 和OutputStream 类表示。因为面向字节的流不方便用来处理存储为Unicode (每个字符使用两个字节) 的信息。所以Java 引入了用来处理Unicode 字符的类层次,这些类派生自抽象类Reader 和Writer ,它们用于读写双字节的Unicode 字符,而不是单字节字符。
Java.io 包简介
JDK 标准帮助文档是这样解释Java.io 包的,通过数据流、序列和文件系统为系统提供输入输出。
InputStream 类和OutputStream 类
InputStream 类是所有输入数据流的父类,它是一个抽象类,定义了所有Java 输入数据流都具有的共通特性。
java.io.InputStream 的方法如下:
1. public abstract read()throws IOException
读取一个字节并返回该字节,如果到输入源的末则返回-1 。一个具体的Java 输入数据流需要重载此方法,以提供 有用的功能。例如:在FileInputStream 类中,该方法从一个文件读取一个字节。
1. public int read(byte[] b)throws IOException
把数据读入到一个字节数据中,并返回实际读取的字节数目。如果遇到流末 则返回-1 ,该方法最多读取b.length 个字节。
1. public abstract int read(byte[] b,int off,int len)throws IOException
把数据读入到一个字节数组中并返回实际读取的字节数目。如果遇到流的末尾则的返回-1 。 其中参数off 表示第一个字节在b 中的位置,len 表示读取的最大字节数。
1. public long skip(long n)throws IOException
略过N 个字节不读取,会返回实际略过的字节数目。因为数据流中剩下的数据可能不到N 个字节那么多,所以此时返回值会小于N 。
1. public int available()throws IOException
read 方法( 包括后面要讲的OutputStream 类的Write 方法) 都能够阴塞一个线程,直到字节被 实际读取或写入。这意味着如果一个流不能立即被读或被写
6. package mytestfiles;
7. import java.io.BufferedReader;
8. import java.io.File;
9. import java.io.FileReader;
10. import java.io.FileWriter;
11. import java.io.IOException;
12. import java.io.PrintWriter;
13.
14. /**
15. * @author zhangqinglin
16. * To change the template for this generated type comment go to
17. * Window > Preferences > Java > Code Generation > Code and Comments
18. */
19. public class Files
20. {
21. public static void main(String[] args) throws IOException
22. {
23. Files f = new Files();
24. // System.out.println(f.readFile("f://LinkFile.java"));
25. // f.readAllFile("f://","LinkFile.java");
26. // f.readLineFile("f://","LinkFile.java");
27. // System.out.println(f.fileIsNull("f://","122.txt"));
28. // f.readFolderByFile("F://PDF");
29. // System.out.println(f.createAndDeleteFolder("ss","f://"));
30. // System.out.println(f.createAndDeleteFile("f://ss//","TestFile.dat"));
31. String[] ss = new String[50];
32. for(int i = 0 ;i{
33. ss[i] = " 信息技术和互联网( 计算机软硬件, 通讯) "+i;
34. }
35. f.writeFile("f://ss//","TestFile.txt",ss);
36. }
37. /**
38. * 文件的写入
39. * @param filePath( 文件路径)
40. * @param fileName( 文件名)
41. * @param args[]
42. * @throws IOException
43. */
44. public void writeFile(String filePath,String fileName,String[] args) throws IOException
45. {
46. FileWriter fw = new FileWriter(filePath+fileName);
47. PrintWriter out = new PrintWriter(fw);
48. for(int i = 0 ;i{
49. out.write(args[i]);
50. out.println();
51. out.flush();
52. }
53. fw.close();
54. out.close();
55. }
56. /**
57. * 文件的写入
58. * @param filePath( 文件路径)
59. * @param fileName( 文件名)
60. * @param args
61. * @throws IOException
62. */
63. public void writeFile(String filePath,String fileName,String args) throws IOException
64. {
65. FileWriter fw = new FileWriter(filePath+fileName);
66. fw.write(args);
67. fw.close();
68. }
69. /**
70. * 创建与删除文件
71. * @param filePath
72. * @param fileName
73. * @return 创建成功返回true
74. * @throws IOException
75. */
76. public boolean createAndDeleteFile(String filePath,String fileName) throws IOException
77. {
78. boolean result = false ;
79. File file = new File(filePath,fileName);
80. if(file.exists())
81. {
82. file.delete();
83. result = true ;
84. System.out.println(" 文件已经删除!");
85. }
86. else
87. {
88. file.createNewFile();
89. result = true ;
90. System.out.println(" 文件已经创建!");
91. }
92. return result;
93. }
94. /**
95. * 创建和删除目录
96. * @param folderName
97. * @param filePath
98. * @return 删除成功返回true
99. */
100. public boolean createAndDeleteFolder(String folderName,String filePath)
101. {
102. boolean result = false ;
103. try
104. {
105. File file = new File(filePath+folderName);
106. if(file.exists())
107. {
108. file.delete();
109. System.out.println(" 目录已经存在,已删除!");
110. result = true ;
111. }
112. else
113. {
114. file.mkdir();
115. System.out.println(" 目录不存在,已经建立!");
116. result = true ;
117. }
118. }
119. catch(Exception ex)
120. {
121. result = false ;
122. System.out.println("CreateAndDeleteFolder is error:"+ex);
123. }
124. return result;
125. }
126. /**
127. * 输出目录中的所有文件及目录名字
128. * @param filePath
129. */
130. public void readFolderByFile(String filePath)
131. {
132. File file = new File(filePath);
133. File[] tempFile = file .listFiles();
134. for(int i = 0 ;i{
135. if(tempFile[i].isFile())
136. {
137. System.out.println("File : "+tempFile[i].getName());
138. }
139. if(tempFile[i].isDirectory())
140. {
141. System.out.println("Directory : "+tempFile[i].getName());
142. }
143. }
144. }
145. /**
146. * 检查文件中是否为一个空
147. * @param filePath
148. * @param fileName
149. * @return 为空返回true
150. * @throws IOException
151. */
152. public boolean fileIsNull(String filePath,String fileName) throws IOException
153. {
154. boolean result = false ;
155. FileReader fr = new FileReader(filePath+fileName);
156. if(fr.read() == -1)
157. {
158. result = true ;
159. System.out.println(fileName+" 文件中没有数据!");
160. }
161. else
162. {
163. System.out.println(fileName+" 文件中有数据!");
164. }
165. fr.close();
166. return result;
167. }
168. /**
169. * 读取文件中的所有内容
170. * @param filePath
171. * @param fileName
172. * @throws IOException
173. */
174. public void readAllFile(String filePath,String fileName) throws IOException
175. {
176. FileReader fr = new FileReader(filePath+fileName);
177. int count = fr .read();
178. while(count != -1)
179. {
180. System.out.print((char)count);
181. count = fr .read();
182. if( count == 13)
183. {
184. fr.skip(1);
185. }
186. }
187. fr.close();
188. }
189. /**
190. * 一行一行的读取文件中的数据
191. * @param filePath
192. * @param fileName
193. * @throws IOException
194. */
195. public void readLineFile(String filePath,String fileName) throws IOException
196. {
197. FileReader fr = new FileReader(filePath+fileName);
198. BufferedReader br = new BufferedReader(fr);
199. String line = br .readLine();
200. while(line != null)
201. {
202. System.out.println(line);
203. line = br .readLine();
204. }
205. br.close();
206. fr.close();
207. }
208. }
到这里Java 输入数据流就介绍完了