Java中的IO整理完整版(一)
【案例1】創建一個新文件
1 import java.io.*;
2 class hello{
3 public static void main(String[] args) {
4 File f=new File("D:\\hello.txt");
5 try{
6 f.createNewFile();
7 }catch (Exception e) {
8 e.printStackTrace();
9 }
10 }
11 }
【運行結果】:
程序運行之後,在d盤下會有一個名字爲hello.txt的文件。
【案例2】File類的兩個常量
12 import java.io.*;
13 class hello{
14 public static void main(String[] args) {
15 System.out.println(File.separator);
16 System.out.println(File.pathSeparator);
17 }
18 }
【運行結果】:
\
;
此處多說幾句:有些同學可能認爲,我直接在windows下使用\進行分割不行嗎?當然是可以的。但是在linux下就不是\了。所以,要想使得我們的代碼跨平臺,更加健壯,所以,大家都採用這兩個常量吧,其實也多寫不了幾行。呵呵、
現在我們使用File類中的常量改寫上面的代碼:
19 import java.io.*;
20 class hello{
21 public static void main(String[] args) {
22 String fileName="D:"+File.separator+"hello.txt";
23 File f=new File(fileName);
24 try{
25 f.createNewFile();
26 }catch (Exception e) {
27 e.printStackTrace();
28 }
29 }
30 }
你看,沒有多寫多少吧,呵呵。所以建議使用File類中的常量。
刪除一個文件
31 /**
32 * 刪除一個文件
33 * */
34 import java.io.*;
35 class hello{
36 public static void main(String[] args) {
37 String fileName="D:"+File.separator+"hello.txt";
38 File f=new File(fileName);
39 if(f.exists()){
40 f.delete();
41 }else{
42 System.out.println("文件不存在");
43 }
44
45 }
46 }
創建一個文件夾
47 /**
48 * 創建一個文件夾
49 * */
50 import java.io.*;
51 class hello{
52 public static void main(String[] args) {
53 String fileName="D:"+File.separator+"hello";
54 File f=new File(fileName);
55 f.mkdir();
56 }
57 }
【運行結果】:
D盤下多了一個hello文件夾
列出指定目錄的全部文件(包括隱藏文件):
58 /**
59 * 使用list列出指定目錄的全部文件
60 * */
61 import java.io.*;
62 class hello{
63 public static void main(String[] args) {
64 String fileName="D:"+File.separator;
65 File f=new File(fileName);
66 String[] str=f.list();
67 for (int i = 0; i < str.length; i++) {
68 System.out.println(str[i]);
69 }
70 }
71 }
【運行結果】:
$RECYCLE.BIN
360
360Downloads
360Rec
360SoftMove
Config.Msi
da
Downloads
DriversBackup
eclipse
java web整合開發和項目實戰
Lenovo
MSOCache
Program
Program Files
python
RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}
System Volume Information
Tomcat6
var
vod_cache_data
新建文件夾
(你的運行結果應該和這個不一樣的,呵呵)
但是使用list返回的是String數組,。而且列出的不是完整路徑,如果想列出完整路徑的話,需要使用listFiles.他返回的是File的數組
列出指定目錄的全部文件(包括隱藏文件):
72 /**
73 * 使用listFiles列出指定目錄的全部文件
74 * listFiles輸出的是完整路徑
75 * */
76 import java.io.*;
77 class hello{
78 public static void main(String[] args) {
79 String fileName="D:"+File.separator;
80 File f=new File(fileName);
81 File[] str=f.listFiles();
82 for (int i = 0; i < str.length; i++) {
83 System.out.println(str[i]);
84 }
85 }
86 }
【運行結果】:
D:\$RECYCLE.BIN
D:\360
D:\360Downloads
D:\360Rec
D:\360SoftMove
D:\Config.Msi
D:\da
D:\Downloads
D:\DriversBackup
D:\eclipse
D:\java web整合開發和項目實戰
D:\Lenovo
D:\MSOCache
D:\Program
D:\Program Files
D:\python
D:\RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}
D:\System Volume Information
D:\Tomcat6
D:\var
D:\vod_cache_data
D:\新建文件夾
通過比較可以指定,使用listFiles更加方便、
判斷一個指定的路徑是否爲目錄
87 /**
88 * 使用isDirectory判斷一個指定的路徑是否爲目錄
89 * */
90 import java.io.*;
91 class hello{
92 public static void main(String[] args) {
93 String fileName="D:"+File.separator;
94 File f=new File(fileName);
95 if(f.isDirectory()){
96 System.out.println("YES");
97 }else{
98 System.out.println("NO");
99 }
100 }
101 }
【運行結果】:YES
搜索指定目錄的全部內容
102 /**
103 * 列出指定目錄的全部內容
104 * */
105 import java.io.*;
106 class hello{
107 public static void main(String[] args) {
108 String fileName="D:"+File.separator;
109 File f=new File(fileName);
110 print(f);
111 }
112 public static void print(File f){
113 if(f!=null){
114 if(f.isDirectory()){
115 File[] fileArray=f.listFiles();
116 if(fileArray!=null){
117 for (int i = 0; i < fileArray.length; i++) {
118 //遞歸調用
119 print(fileArray[i]);
120 }
121 }
122 }
123 else{
124 System.out.println(f);
125 }
126 }
127 }
128 }
【運行結果】:
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\framepages\web4welcome_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\transit_jsp.class
……
【使用RandomAccessFile寫入文件】
129 /**
130 * 使用RandomAccessFile寫入文件
131 * */
132 import java.io.*;
133 class hello{
134 public static void main(String[] args) throws IOException {
135 String fileName="D:"+File.separator+"hello.txt";
136 File f=new File(fileName);
137 RandomAccessFile demo=new RandomAccessFile(f,"rw");
138 demo.writeBytes("asdsad");
139 demo.writeInt(12);
140 demo.writeBoolean(true);
141 demo.writeChar('A');
142 demo.writeFloat(1.21f);
143 demo.writeDouble(12.123);
144 demo.close();
145 }
146 }
如果你此時打開hello。txt查看的話,會發現那是亂碼。
字節流
【向文件中寫入字符串】
147 /**
148 * 字節流
149 * 向文件中寫入字符串
150 * */
151 import java.io.*;
152 class hello{
153 public static void main(String[] args) throws IOException {
154 String fileName="D:"+File.separator+"hello.txt";
155 File f=new File(fileName);
156 OutputStream out =new FileOutputStream(f);
157 String str="你好";
158 byte[] b=str.getBytes();
159 out.write(b);
160 out.close();
161 }
162 }
查看hello.txt會看到“你好”
當然也可以一個字節一個字節的寫。
163 /**
164 * 字節流
165 * 向文件中一個字節一個字節的寫入字符串
166 * */
167 import java.io.*;
168 class hello{
169 public static void main(String[] args) throws IOException {
170 String fileName="D:"+File.separator+"hello.txt";
171 File f=new File(fileName);
172 OutputStream out =new FileOutputStream(f);
173 String str="你好";
174 byte[] b=str.getBytes();
175 for (int i = 0; i < b.length; i++) {
176 out.write(b[i]);
177 }
178 out.close();
179 }
180 }
結果還是:“你好”
向文件中追加新內容:
181 /**
182 * 字節流
183 * 向文件中追加新內容:
184 * */
185 import java.io.*;
186 class hello{
187 public static void main(String[] args) throws IOException {
188 String fileName="D:"+File.separator+"hello.txt";
189 File f=new File(fileName);
190 OutputStream out =new FileOutputStream(f,true);
191 String str="Rollen";
192 //String str="\r\nRollen"; 可以換行
193 byte[] b=str.getBytes();
194 for (int i = 0; i < b.length; i++) {
195 out.write(b[i]);
196 }
197 out.close();
198 }
199 }
【運行結果】:
你好Rollen
【讀取文件內容】
200 /**
201 * 字節流
202 * 讀文件內容
203 * */
204 import java.io.*;
205 class hello{
206 public static void main(String[] args) throws IOException {
207 String fileName="D:"+File.separator+"hello.txt";
208 File f=new File(fileName);
209 InputStream in=new FileInputStream(f);
210 byte[] b=new byte[1024];
211 in.read(b);
212 in.close();
213 System.out.println(new String(b));
214 }
215 }
【運行結果】
你好Rollen
Rollen_
但是這個例子讀取出來會有大量的空格,我們可以利用in.read(b);的返回值來設計程序。如下:
216 /**
217 * 字節流
218 * 讀文件內容
219 * */
220 import java.io.*;
221 class hello{
222 public static void main(String[] args) throws IOException {
223 String fileName="D:"+File.separator+"hello.txt";
224 File f=new File(fileName);
225 InputStream in=new FileInputStream(f);
226 byte[] b=new byte[1024];
227 int len=in.read(b);
228 in.close();
229 System.out.println("讀入長度爲:"+len);
230 System.out.println(new String(b,0,len));
231 }
232 }
【運行結果】:
讀入長度爲:18
你好Rollen
Rollen
讀者觀察上面的例子可以看出,我們預先申請了一個指定大小的空間,但是有時候這個空間可能太小,有時候可能太大,我們需要準確的大小,這樣節省空間,那麼我們可以這樣幹:
233 /**
234 * 字節流
235 * 讀文件內容,節省空間
236 * */
237 import java.io.*;
238 class hello{
239 public static void main(String[] args) throws IOException {
240 String fileName="D:"+File.separator+"hello.txt";
241 File f=new File(fileName);
242 InputStream in=new FileInputStream(f);
243 byte[] b=new byte[(int)f.length()];
244 in.read(b);
245 System.out.println("文件長度爲:"+f.length());
246 in.close();
247 System.out.println(new String(b));
248 }
249 }
文件長度爲:18
你好Rollen
Rollen
將上面的例子改爲一個一個讀:
250 /**
251 * 字節流
252 * 讀文件內容,節省空間
253 * */
254 import java.io.*;
255 class hello{
256 public static void main(String[] args) throws IOException {
257 String fileName="D:"+File.separator+"hello.txt";
258 File f=new File(fileName);
259 InputStream in=new FileInputStream(f);
260 byte[] b=new byte[(int)f.length()];
261 for (int i = 0; i < b.length; i++) {
262 b[i]=(byte)in.read();
263 }
264 in.close();
265 System.out.println(new String(b));
266 }
267 }
輸出的結果和上面的一樣。
細心的讀者可能會發現,上面的幾個例子都是在知道文件的內容多大,然後才展開的,有時候我們不知道文件有多大,這種情況下,我們需要判斷是否獨到文件的末尾。
268 /**
269 * 字節流
270 *讀文件
271 * */
272 import java.io.*;
273 class hello{
274 public static void main(String[] args) throws IOException {
275 String fileName="D:"+File.separator+"hello.txt";
276 File f=new File(fileName);
277 InputStream in=new FileInputStream(f);
278 byte[] b=new byte[1024];
279 int count =0;
280 int temp=0;
281 while((temp=in.read())!=(-1)){
282 b[count++]=(byte)temp;
283 }
284 in.close();
285 System.out.println(new String(b));
286 }
287 }
【運行結果】
你好Rollen
Rollen_
提醒一下,當獨到文件末尾的時候會返回-1.正常情況下是不會返回-1的
現在我們使用字符流
288 /**
289 * 字符流
290 * 寫入數據
291 * */
292 import java.io.*;
293 class hello{
294 public static void main(String[] args) throws IOException {
295 String fileName="D:"+File.separator+"hello.txt";
296 File f=new File(fileName);
297 Writer out =new FileWriter(f);
298 String str="hello";
299 out.write(str);
300 out.close();
301 }
302 }
當你打開hello。txt的時候,會看到hello
其實這個例子上之前的例子沒什麼區別,只是你可以直接輸入字符串,而不需要你將字符串轉化爲字節數組。
當你如果想問文件中追加內容的時候,可以使用將上面的聲明out的哪一行換爲:
Writer out =new FileWriter(f,true);
這樣,當你運行程序的時候,會發現文件內容變爲:
hellohello如果想在文件中換行的話,需要使用“\r\n”
比如將str變爲String str="\r\nhello";
這樣文件追加的str的內容就會換行了。
從文件中讀內容:
303 /**
304 * 字符流
305 * 從文件中讀出內容
306 * */
307 import java.io.*;
308 class hello{
309 public static void main(String[] args) throws IOException {
310 String fileName="D:"+File.separator+"hello.txt";
311 File f=new File(fileName);
312 char[] ch=new char[100];
313 Reader read=new FileReader(f);
314 int count=read.read(ch);
315 read.close();
316 System.out.println("讀入的長度爲:"+count);
317 System.out.println("內容爲"+new String(ch,0,count));
318 }
319 }
【運行結果】:
讀入的長度爲:17
內容爲hellohello
hello
當然最好採用循環讀取的方式,因爲我們有時候不知道文件到底有多大。
320 /**
321 * 字符流
322 * 從文件中讀出內容
323 * */
324 import java.io.*;
325 class hello{
326 public static void main(String[] args) throws IOException {
327 String fileName="D:"+File.separator+"hello.txt";
328 File f=new File(fileName);
329 char[] ch=new char[100];
330 Reader read=new FileReader(f);
331 int temp=0;
332 int count=0;
333 while((temp=read.read())!=(-1)){
334 ch[count++]=(char)temp;
335 }
336 read.close();
337 System.out.println("內容爲"+new String(ch,0,count));
338 }
339 }
運行結果:
內容爲hellohello
hello
關於字節流和字符流的區別
實際上字節流在操作的時候本身是不會用到緩衝區的,是文件本身的直接操作的,但是字符流在操作的 時候下後是會用到緩衝區的,是通過緩衝區來操作文件的。
讀者可以試着將上面的字節流和字符流的程序的最後一行關閉文件的代碼註釋掉,然後運行程序看看。你就會發現使用字節流的話,文件中已經存在內容,但是使用字符流的時候,文件中還是沒有內容的,這個時候就要刷新緩衝區。
使用字節流好還是字符流好呢?
答案是字節流。首先因爲硬盤上的所有文件都是以字節的形式進行傳輸或者保存的,包括圖片等內容。但是字符只是在內存中才會形成的,所以在開發中,字節流使用廣泛。
文件的複製
其實DOS下就有一個文件複製功能,比如我們想把d盤下面的hello.txt文件複製到d盤下面的rollen.txt文件中,那麼我們就可以使用下面的命令:
copy d:\hello.txt d:\rollen.txt
運行之後你會在d盤中看見hello.txt.,並且兩個文件的內容是一樣的,(這是屁話)
下面我們使用程序來複制文件吧。
基本思路還是從一個文件中讀入內容,邊讀邊寫入另一個文件,就是這麼簡單。、
首先編寫下面的代碼:
340 /**
341 * 文件的複製
342 * */
343 import java.io.*;
344 class hello{
345 public static void main(String[] args) throws IOException {
346 if(args.length!=2){
347 System.out.println("命令行參數輸入有誤,請檢查");
348 System.exit(1);
349 }
350 File file1=new File(args[0]);
351 File file2=new File(args[1]);
352
353 if(!file1.exists()){
354 System.out.println("被複制的文件不存在");
355 System.exit(1);
356 }
357 InputStream input=new FileInputStream(file1);
358 OutputStream output=new FileOutputStream(file2);
359 if((input!=null)&&(output!=null)){
360 int temp=0;
361 while((temp=input.read())!=(-1)){
362 output.write(temp);
363 }
364 }
365 input.close();
366 output.close();
367 }
368 }
然後在命令行下面
javac hello.java
java hello d:\hello.txt d:\rollen.txt
現在你就會在d盤看到rollen。txt了,
OutputStreramWriter 和InputStreamReader類
整個IO類中除了字節流和字符流還包括字節和字符轉換流。
OutputStreramWriter將輸出的字符流轉化爲字節流
InputStreamReader將輸入的字節流轉換爲字符流
但是不管如何操作,最後都是以字節的形式保存在文件中的。
將字節輸出流轉化爲字符輸出流
369 /**
370 * 將字節輸出流轉化爲字符輸出流
371 * */
372 import java.io.*;
373 class hello{
374 public static void main(String[] args) throws IOException {
375 String fileName= "d:"+File.separator+"hello.txt";
File file=new File(file
376 Writer out=new OutputStreamWriter(new FileOutputStream(file));
377 out.write("hello");
378 out.close();
379 }
380 }
運行結果:文件中內容爲:hello
將字節輸入流變爲字符輸入流
381 /**
382 * 將字節輸入流變爲字符輸入流
383 * */
384 import java.io.*;
385 class hello{
386 public static void main(String[] args) throws IOException {
387 String fileName= "d:"+File.separator+"hello.txt";
388 File file=new File(fileName);
389 Reader read=new InputStreamReader(new FileInputStream(file));
390 char[] b=new char[100];
391 int len=read.read(b);
392 System.out.println(new String(b,0,len));
393 read.close();
394 }
395 }
【運行結果】:hello
前面列舉的輸出輸入都是以文件進行的,現在我們以內容爲輸出輸入目的地,使用內存操作流
ByteArrayInputStream 主要將內容寫入內容
ByteArrayOutputStream 主要將內容從內存輸出
使用內存操作流將一個大寫字母轉化爲小寫字母
396 /**
397 * 使用內存操作流將一個大寫字母轉化爲小寫字母
398 * */
399 import java.io.*;
400 class hello{
401 public static void main(String[] args) throws IOException {
402 String str="ROLLENHOLT";
403 ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
404 ByteArrayOutputStream output=new ByteArrayOutputStream();
405 int temp=0;
406 while((temp=input.read())!=-1){
407 char ch=(char)temp;
408 output.write(Character.toLowerCase(ch));
409 }
410 String outStr=output.toString();
411 input.close();
412 output.close();
413 System.out.println(outStr);
414 }
415 }
【運行結果】:
rollenholt
內容操作流一般使用來生成一些臨時信息採用的,這樣可以避免刪除的麻煩。
管道流
管道流主要可以進行兩個線程之間的通信。
PipedOutputStream 管道輸出流
PipedInputStream 管道輸入流
驗證管道流
416 /**
417 * 驗證管道流
418 * */
419 import java.io.*;
420
421 /**
422 * 消息發送類
423 * */
424 class Send implements Runnable{
425 private PipedOutputStream out=null;
426 public Send() {
427 out=new PipedOutputStream();
428 }
429 public PipedOutputStream getOut(){
430 return this.out;
431 }
432 public void run(){
433 String message="hello , Rollen";
434 try{
435 out.write(message.getBytes());
436 }catch (Exception e) {
437 e.printStackTrace();
438 }try{
439 out.close();
440 }catch (Exception e) {
441 e.printStackTrace();
442 }
443 }
444 }
445
446 /**
447 * 接受消息類
448 * */
449 class Recive implements Runnable{
450 private PipedInputStream input=null;
451 public Recive(){
452 this.input=new PipedInputStream();
453 }
454 public PipedInputStream getInput(){
455 return this.input;
456 }
457 public void run(){
458 byte[] b=new byte[1000];
459 int len=0;
460 try{
461 len=this.input.read(b);
462 }catch (Exception e) {
463 e.printStackTrace();
464 }try{
465 input.close();
466 }catch (Exception e) {
467 e.printStackTrace();
468 }
469 System.out.println("接受的內容爲 "+(new String(b,0,len)));
470 }
471 }
472 /**
473 * 測試類
474 * */
475 class hello{
476 public static void main(String[] args) throws IOException {
477 Send send=new Send();
478 Recive recive=new Recive();
479 try{
480 //管道連接
481 send.getOut().connect(recive.getInput());
482 }catch (Exception e) {
483 e.printStackTrace();
484 }
485 new Thread(send).start();
486 new Thread(recive).start();
487 }
488 }
【運行結果】:
接受的內容爲 hello , Rollen
打印流
489 /**
490 * 使用PrintStream進行輸出
491 * */
492 import java.io.*;
493
494 class hello {
495 public static void main(String[] args) throws IOException {
496 PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
497 + File.separator + "hello.txt")));
498 print.println(true);
499 print.println("Rollen");
500 print.close();
501 }
502 }
【運行結果】:
true
Rollen
當然也可以格式化輸出
503 /**
504 * 使用PrintStream進行輸出
505 * 並進行格式化
506 * */
507 import java.io.*;
508 class hello {
509 public static void main(String[] args) throws IOException {
510 PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
511 + File.separator + "hello.txt")));
512 String name="Rollen";
513 int age=20;
514 print.printf("姓名:%s. 年齡:%d.",name,age);
515 print.close();
516 }
517 }
【運行結果】:
姓名:Rollen. 年齡:20.
使用OutputStream向屏幕上輸出內容
518 /**
519 * 使用OutputStream向屏幕上輸出內容
520 * */
521 import java.io.*;
522 class hello {
523 public static void main(String[] args) throws IOException {
524 OutputStream out=System.out;
525 try{
526 out.write("hello".getBytes());
527 }catch (Exception e) {
528 e.printStackTrace();
529 }
530 try{
531 out.close();
532 }catch (Exception e) {
533 e.printStackTrace();
534 }
535 }
536 }
【運行結果】:
hello
輸入輸出重定向
537 import java.io.File;
538 import java.io.FileNotFoundException;
539 import java.io.FileOutputStream;
540 import java.io.PrintStream;
541
542 /**
543 * 爲System.out.println()重定向輸出
544 * */
545 public class systemDemo{
546 public static void main(String[] args){
547 // 此刻直接輸出到屏幕
548 System.out.println("hello");
549 File file = new File("d:" + File.separator + "hello.txt");
550 try{
551 System.setOut(new PrintStream(new FileOutputStream(file)));
552 }catch(FileNotFoundException e){
553 e.printStackTrace();
554 }
555 System.out.println("這些內容在文件中才能看到哦!");
556 }
557 }
【運行結果】:
eclipse的控制檯輸出的是hello。然後當我們查看d盤下面的hello.txt文件的時候,會在裏面看到:這些內容在文件中才能看到哦!
558 import java.io.File;
559 import java.io.FileNotFoundException;
560 import java.io.FileOutputStream;
561 import java.io.PrintStream;
562
563 /**
564 * System.err重定向 這個例子也提示我們可以使用這種方法保存錯誤信息
565 * */
566 public class systemErr{
567 public static void main(String[] args){
568 File file = new File("d:" + File.separator + "hello.txt");
569 System.err.println("這些在控制檯輸出");
570 try{
571 System.setErr(new PrintStream(new FileOutputStream(file)));
572 }catch(FileNotFoundException e){
573 e.printStackTrace();
574 }
575 System.err.println("這些在文件中才能看到哦!");
576 }
577 }
【運行結果】:
你會在eclipse的控制檯看到紅色的輸出:“這些在控制檯輸出”,然後在d盤下面的hello.txt中會看到:這些在文件中才能看到哦!
578 import java.io.File;
579 import java.io.FileInputStream;
580 import java.io.FileNotFoundException;
581 import java.io.IOException;
582
583 /**
584 * System.in重定向
585 * */
586 public class systemIn{
587 public static void main(String[] args){
588 File file = new File("d:" + File.separator + "hello.txt");
589 if(!file.exists()){
590 return;
591 }else{
592 try{
593 System.setIn(new FileInputStream(file));
594 }catch(FileNotFoundException e){
595 e.printStackTrace();
596 }
597 byte[] bytes = new byte[1024];
598 int len = 0;
599 try{
600 len = System.in.read(bytes);
601 }catch(IOException e){
602 e.printStackTrace();
603 }
604 System.out.println("讀入的內容爲:" + new String(bytes, 0, len));
605 }
606 }
607 }
【運行結果】:
前提是我的d盤下面的hello.txt中的內容是:“這些文件中的內容哦!”,然後運行程序,輸出的結果爲:讀入的內容爲:這些文件中的內容哦