java中讀取txt文件的三種方式
1. package com.readfile;
2.
3. import java.io.BufferedReader;
4. import java.io.File;
5. import java.io.FileInputStream;
6. import java.io.FileReader;
7. import java.io.IOException;
8. import java.io.InputStream;
9. import java.io.InputStreamReader;
10. import java.io.RandomAccessFile;
11. import java.io.Reader;
12.
13. public class ReadFromFile {
14.
15. /**
16. * @param args
17. */
18. public static void main(String[] args) {
19. // TODO Auto-generated method stub
20. String fileName = "C:/Users/Administrator/Desktop/Noname1.txt";
21.
22. //readFileByChars(fileName);
23. //readFileByLines(fileName);
24. readFileByRandomAccess(fileName);
25. }
26.
27. /**
28. * 隨機讀取檔案內容
29. */
30. public static void readFileByRandomAccess(String fileName) {
31. RandomAccessFile randomFile = null;
32. try {
33. System.out.println("隨機讀取一段檔案內容:");
34. // 開啟一個隨機訪問檔案流,按只讀方式
35. randomFile = new RandomAccessFile(fileName, "r");
36. // 檔案長度,位元組數
37. long fileLength = randomFile.length();
38. // 讀檔案的起始位置
39. int beginIndex = (fileLength > 4) ? 0 : 0;
40. // 將讀檔案的開始位置移到beginIndex位置。
41. randomFile.seek(beginIndex);
42. byte[] bytes = new byte[10];
43. int byteread = 0;
44. // 一次讀10個位元組,如果檔案內容不足10個位元組,則讀剩下的位元組。
45. // 將一次讀取的位元組數賦給byteread
46. while ((byteread = randomFile.read(bytes)) != -1) {
47. System.out.write(bytes, 0, byteread);
48. }
49. } catch (IOException e) {
50. e.printStackTrace();
51. } finally {
52. if (randomFile != null) {
53. try {
54. randomFile.close();
55. } catch (IOException e1) {
56. }
57. }
58. }
59. }
60. /**
61. * 以行為單位讀取檔案,常用於讀面向行的格式化檔案
62. */
63. public static void readFileByLines(String fileName) {
64. File file = new File(fileName);
65. BufferedReader reader = null;
66. try {
67. System.out.println("以行為單位讀取檔案內容,一次讀一整行:");
68. reader = new BufferedReader(new FileReader(file));
69. String tempString = null;
70. int line = 1;
71. // 一次讀入一行,直到讀入null為檔案結束
72. while ((tempString = reader.readLine()) != null) {
73. // 顯示行號
74. System.out.println("line " + line + ": " + tempString);
75. line++;
76. }
77. reader.close();
78. } catch (IOException e) {
79. e.printStackTrace();
80. } finally {
81. if (reader != null) {
82. try {
83. reader.close();
84. } catch (IOException e1) {
85. }
86. }
87. }
88. }
89.
90. /**
91. * 以字元為單位讀取檔案,常用於讀文字,數字等型別的檔案
92. */
93. public static void readFileByChars(String fileName) {
94. File file = new File(fileName);
95. Reader reader = null;
96. try {
97. System.out.println("以字元為單位讀取檔案內容,一次讀一個位元組:");
98. // 一次讀一個字元
99. reader = new InputStreamReader(new FileInputStream(file));
100. int tempchar;
101. while ((tempchar = reader.read()) != -1) {
102. // 對於windows下,\r\n這兩個字元在一起時,表示一個換行。
103. // 但如果這兩個字元分開顯示時,會換兩次行。
104. // 因此,遮蔽掉\r,或者遮蔽\n。否則,將會多出很多空行。
105. if (((char) tempchar) != '\r') {
106. System.out.print((char) tempchar);
107. }
108. }
109. reader.close();
110. } catch (Exception e) {
111. e.printStackTrace();
112. }
113. try {
114. System.out.println("\n以字元為單位讀取檔案內容,一次讀多個位元組:");
115. // 一次讀多個字元
116. char[] tempchars = new char[30];
117. int charread = 0;
118. reader = new InputStreamReader(new FileInputStream(fileName));
119. // 讀入多個字元到字元陣列中,charread為一次讀取字元數
120. while ((charread = reader.read(tempchars)) != -1) {
121. // 同樣遮蔽掉\r不顯示
122. if ((charread == tempchars.length)
123. && (tempchars[tempchars.length - 1] != '\r')) {
124. System.out.print(tempchars);
125. } else {
126. for (int i = 0; i < charread; i++) {
127. if (tempchars[i] == '\r') {
128. continue;
129. } else {
130. System.out.print(tempchars[i]);
131. }
132. }
133. }
134. }
135.
136. } catch (Exception e1) {
137. e1.printStackTrace();
138. } finally {
139. if (reader != null) {
140. try {
141. reader.close();
142. } catch (IOException e1) {
143. }
144. }
145. }
146. }
147. /**
148. * 以位元組為單位讀取檔案,常用於讀二進位制檔案,如圖片、聲音、影像等檔案。
149. */
150. public static void readFileByBytes(String fileName) {
151. File file = new File(fileName);
152. InputStream in = null;
153. try {
154. System.out.println("以位元組為單位讀取檔案內容,一次讀一個位元組:");
155. // 一次讀一個位元組
156. in = new FileInputStream(file);
157. int tempbyte;
158. while ((tempbyte = in.read())!=-1) {
159. System.out.println(tempbyte);
160. }
161. } catch (Exception e) {
162. // TODO: handle exception
163. e.printStackTrace();
164. }
165.
166. try {
167. System.out.println("以位元組為單位讀取檔案內容,一次讀多個位元組:");
168. // 一次讀多個位元組
169. byte[] tempbytes = new byte[100];
170. int byteread = 0;
171. in = new FileInputStream(fileName);
172. ReadFromFile.showAvailableBytes(in);
173. // 讀入多個位元組到位元組陣列中,byteread為一次讀入的位元組數
174. while ((byteread = in.read(tempbytes)) != -1) {
175. System.out.write(tempbytes, 0, byteread);//好方法,第一個引數是陣列,第二個引數是開始位置,第三個引數是長度
176. }
177. } catch (Exception e1) {
178. e1.printStackTrace();
179. } finally {
180. if (in != null) {
181. try {
182. in.close();
183. } catch (IOException e1) {
184. }
185. }
186. }
187. }
188.
189. /**
190. * 顯示輸入流中還剩的位元組數
191. */
192. private static void showAvailableBytes(InputStream in) {
193. try {
194. System.out.println("當前位元組輸入流中的位元組數為:" + in.available());
195. } catch (IOException e) {
196. e.printStackTrace();
197. }
198. }
199.
200. }