【案例1】創建一個新文件
import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????File?f=new?File("D:\\hello.txt"); ?????????try{ ?????????????f.createNewFile(); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????} ?}?【運行結果】:
程序運行之后,在d盤下會有一個名字為hello.txt的文件。
【案例2】File類的兩個常量
import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????System.out.println(File.separator); ?????????System.out.println(File.pathSeparator); ?????} ?}?【運行結果】:
\
;
此處多說幾句:有些同學可能認為,我直接在windows下使用\進行分割不行嗎?當然是可以的。但是在linux下就不是\了。所以,要想使得我們的代碼跨平臺,更加健壯,所以,大家都采用這兩個常量吧,其實也多寫不了幾行。呵呵、
現在我們使用File類中的常量改寫上面的代碼:
import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????try{ ?????????????f.createNewFile(); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????} ?}?你看,沒有多寫多少吧,呵呵。所以建議使用File類中的常量。
刪除一個文件
/** ??*?刪除一個文件 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????if(f.exists()){ ?????????????f.delete(); ?????????}else{ ?????????????System.out.println("文件不存在"); ?????????} ????????? ?????} ?}?創建一個文件夾
/** ??*?創建一個文件夾 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator+"hello"; ?????????File?f=new?File(fileName); ?????????f.mkdir(); ?????} ?}?【運行結果】:
D盤下多了一個hello文件夾
列出指定目錄的全部文件(包括隱藏文件):
/** ??*?使用list列出指定目錄的全部文件 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator; ?????????File?f=new?File(fileName); ?????????String[]?str=f.list(); ?????????for?(int?i?=?0;?i?<?str.length;?i++)?{ ?????????????System.out.println(str[i]); ?????????} ?????} ?}?【運行結果】:
$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的數組
列出指定目錄的全部文件(包括隱藏文件):
/** ??*?使用listFiles列出指定目錄的全部文件 ??*?listFiles輸出的是完整路徑 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator; ?????????File?f=new?File(fileName); ?????????File[]?str=f.listFiles(); ?????????for?(int?i?=?0;?i?<?str.length;?i++)?{ ?????????????System.out.println(str[i]); ?????????} ?????} ?}?【運行結果】:
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更加方便、
判斷一個指定的路徑是否為目錄
/** ??*?使用isDirectory判斷一個指定的路徑是否為目錄 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator; ?????????File?f=new?File(fileName); ?????????if(f.isDirectory()){ ?????????????System.out.println("YES"); ?????????}else{ ?????????????System.out.println("NO"); ?????????} ?????} ?}?【運行結果】:YES
搜索指定目錄的全部內容
/** ??*?列出指定目錄的全部內容 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?{ ?????????String?fileName="D:"+File.separator; ?????????File?f=new?File(fileName); ?????????print(f); ?????} ?????public?static?void?print(File?f){ ?????????if(f!=null){ ?????????????if(f.isDirectory()){ ?????????????????File[]?fileArray=f.listFiles(); ?????????????????if(fileArray!=null){ ?????????????????????for?(int?i?=?0;?i?<?fileArray.length;?i++)?{ ?????????????????????????//遞歸調用 ?????????????????????????print(fileArray[i]); ?????????????????????} ?????????????????} ?????????????} ?????????????else{ ?????????????????System.out.println(f); ?????????????} ?????????} ?????} ?}?【運行結果】:
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寫入文件】
/** ??*?使用RandomAccessFile寫入文件 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????RandomAccessFile?demo=new?RandomAccessFile(f,"rw"); ?????????demo.writeBytes("asdsad"); ?????????demo.writeInt(12); ?????????demo.writeBoolean(true); ?????????demo.writeChar('A'); ?????????demo.writeFloat(1.21f); ?????????demo.writeDouble(12.123); ?????????demo.close();??? ?????} ?}?如果你此時打開hello。txt查看的話,會發現那是亂碼。
字節流
【向文件中寫入字符串】
/** ??*?字節流 ??*?向文件中寫入字符串 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????OutputStream?out?=new?FileOutputStream(f); ?????????String?str="你好"; ?????????byte[]?b=str.getBytes(); ?????????out.write(b); ?????????out.close(); ?????} ?}?查看hello.txt會看到“你好”
當然也可以一個字節一個字節的寫。
/** ??*?字節流 ??*?向文件中一個字節一個字節的寫入字符串 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????OutputStream?out?=new?FileOutputStream(f); ?????????String?str="你好"; ?????????byte[]?b=str.getBytes(); ?????????for?(int?i?=?0;?i?<?b.length;?i++)?{ ?????????????out.write(b[i]); ?????????} ?????????out.close(); ?????} ?}?結果還是:“你好”
向文件中追加新內容:
/** ??*?字節流 ??*?向文件中追加新內容: ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????OutputStream?out?=new?FileOutputStream(f,true); ?????????String?str="Rollen"; ?????????//String?str="\r\nRollen";??可以換行 ?????????byte[]?b=str.getBytes(); ?????????for?(int?i?=?0;?i?<?b.length;?i++)?{ ?????????????out.write(b[i]); ?????????} ?????????out.close(); ?????} ?}?【運行結果】:
你好Rollen
【讀取文件內容】
/** ??*?字節流 ??*?讀文件內容 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????InputStream?in=new?FileInputStream(f); ?????????byte[]?b=new?byte[1024]; ?????????in.read(b); ?????????in.close(); ?????????System.out.println(new?String(b)); ?????} ?}?【運行結果】
你好Rollen
Rollen_
但是這個例子讀取出來會有大量的空格,我們可以利用in.read(b);的返回值來設計程序。如下:
/** ??*?字節流 ??*?讀文件內容 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????InputStream?in=new?FileInputStream(f); ?????????byte[]?b=new?byte[1024]; ?????????int?len=in.read(b); ?????????in.close(); ?????????System.out.println("讀入長度為:"+len); ?????????System.out.println(new?String(b,0,len)); ?????} ?}?【運行結果】:
讀入長度為:18
你好Rollen
Rollen
讀者觀察上面的例子可以看出,我們預先申請了一個指定大小的空間,但是有時候這個空間可能太小,有時候可能太大,我們需要準確的大小,這樣節省空間,那么我們可以這樣干:
/** ??*?字節流 ??*?讀文件內容,節省空間 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????InputStream?in=new?FileInputStream(f); ?????????byte[]?b=new?byte[(int)f.length()]; ?????????in.read(b); ?????????System.out.println("文件長度為:"+f.length()); ?????????in.close(); ?????????System.out.println(new?String(b)); ?????} ?}?文件長度為:18
你好Rollen
Rollen
將上面的例子改為一個一個讀:
/** ??*?字節流 ??*?讀文件內容,節省空間 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????InputStream?in=new?FileInputStream(f); ?????????byte[]?b=new?byte[(int)f.length()]; ?????????for?(int?i?=?0;?i?<?b.length;?i++)?{ ?????????????b[i]=(byte)in.read(); ?????????} ?????????in.close(); ?????????System.out.println(new?String(b)); ?????} ?}?輸出的結果和上面的一樣。
細心的讀者可能會發現,上面的幾個例子都是在知道文件的內容多大,然后才展開的,有時候我們不知道文件有多大,這種情況下,我們需要判斷是否獨到文件的末尾。
/** ??*?字節流 ??*讀文件 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????InputStream?in=new?FileInputStream(f); ?????????byte[]?b=new?byte[1024]; ?????????int?count?=0; ?????????int?temp=0; ?????????while((temp=in.read())!=(-1)){ ?????????????b[count++]=(byte)temp; ?????????} ?????????in.close(); ?????????System.out.println(new?String(b)); ?????} ?}?【運行結果】
你好Rollen
Rollen_
提醒一下,當獨到文件末尾的時候會返回-1.正常情況下是不會返回-1的
現在我們使用字符流
/** ??*?字符流 ??*?寫入數據 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????Writer?out?=new?FileWriter(f); ?????????String?str="hello"; ?????????out.write(str); ?????????out.close(); ?????} ?}?當你打開hello。txt的時候,會看到hello
其實這個例子上之前的例子沒什么區別,只是你可以直接輸入字符串,而不需要你將字符串轉化為字節數組。
當你如果想問文件中追加內容的時候,可以使用將上面的聲明out的哪一行換為:
Writer out =new FileWriter(f,true);
這樣,當你運行程序的時候,會發現文件內容變為:
hellohello如果想在文件中換行的話,需要使用“\r\n”
比如將str變為String str="\r\nhello";
這樣文件追加的str的內容就會換行了。
從文件中讀內容:
/** ??*?字符流 ??*?從文件中讀出內容 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????char[]?ch=new?char[100]; ?????????Reader?read=new?FileReader(f); ?????????int?count=read.read(ch); ?????????read.close(); ?????????System.out.println("讀入的長度為:"+count); ?????????System.out.println("內容為"+new?String(ch,0,count)); ?????} ?}?【運行結果】:
讀入的長度為:17
內容為hellohello
hello
當然最好采用循環讀取的方式,因為我們有時候不知道文件到底有多大。
/** ??*?字符流 ??*?從文件中讀出內容 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName="D:"+File.separator+"hello.txt"; ?????????File?f=new?File(fileName); ?????????char[]?ch=new?char[100]; ?????????Reader?read=new?FileReader(f); ?????????int?temp=0; ?????????int?count=0; ?????????while((temp=read.read())!=(-1)){ ?????????????ch[count++]=(char)temp; ?????????} ?????????read.close(); ?????????System.out.println("內容為"+new?String(ch,0,count)); ?????} ?}?運行結果:
內容為hellohello
hello
關于字節流和字符流的區別
實際上字節流在操作的時候本身是不會用到緩沖區的,是文件本身的直接操作的,但是字符流在操作的 時候下后是會用到緩沖區的,是通過緩沖區來操作文件的。
讀者可以試著將上面的字節流和字符流的程序的最后一行關閉文件的代碼注釋掉,然后運行程序看看。你就會發現使用字節流的話,文件中已經存在內容,但是使用字符流的時候,文件中還是沒有內容的,這個時候就要刷新緩沖區。
使用字節流好還是字符流好呢?
答案是字節流。首先因為硬盤上的所有文件都是以字節的形式進行傳輸或者保存的,包括圖片等內容。但是字符只是在內存中才會形成的,所以在開發中,字節流使用廣泛。
文件的復制
其實DOS下就有一個文件復制功能,比如我們想把d盤下面的hello.txt文件復制到d盤下面的rollen.txt文件中,那么我們就可以使用下面的命令:
copy d:\hello.txt d:\rollen.txt
運行之后你會在d盤中看見hello.txt.,并且兩個文件的內容是一樣的,(這是屁話)
下面我們使用程序來復制文件吧。
基本思路還是從一個文件中讀入內容,邊讀邊寫入另一個文件,就是這么簡單。、
首先編寫下面的代碼:
/** ??*?文件的復制 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????if(args.length!=2){ ?????????????System.out.println("命令行參數輸入有誤,請檢查"); ?????????????System.exit(1); ?????????} ?????????File?file1=new?File(args[0]); ?????????File?file2=new?File(args[1]); ????????? ?????????if(!file1.exists()){ ?????????????System.out.println("被復制的文件不存在"); ?????????????System.exit(1); ?????????} ?????????InputStream?input=new?FileInputStream(file1); ?????????OutputStream?output=new?FileOutputStream(file2); ?????????if((input!=null)&&(output!=null)){ ?????????????int?temp=0; ?????????????while((temp=input.read())!=(-1)){ ?????????????????output.write(temp); ?????????????} ?????????} ?????????input.close(); ?????????output.close();? ?????} ?}?然后在命令行下面
javac hello.java
java hello d:\hello.txt d:\rollen.txt
現在你就會在d盤看到rollen。txt了,
OutputStreramWriter 和InputStreamReader類
整個IO類中除了字節流和字符流還包括字節和字符轉換流。
OutputStreramWriter將輸出的字符流轉化為字節流
InputStreamReader將輸入的字節流轉換為字符流
但是不管如何操作,最后都是以字節的形式保存在文件中的。
將字節輸出流轉化為字符輸出流
/** ??*?將字節輸出流轉化為字符輸出流 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName=?"d:"+File.separator+"hello.txt"; ?????????File?file=new?File(fileName); ?????????Writer?out=new?OutputStreamWriter(new?FileOutputStream(file)); ?????????out.write("hello"); ?????????out.close(); ?????} ?}?運行結果:文件中內容為:hello
將字節輸入流變為字符輸入流
/** ??*?將字節輸入流變為字符輸入流 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?fileName=?"d:"+File.separator+"hello.txt"; ?????????File?file=new?File(fileName); ?????????Reader?read=new?InputStreamReader(new?FileInputStream(file)); ?????????char[]?b=new?char[100]; ?????????int?len=read.read(b); ?????????System.out.println(new?String(b,0,len)); ?????????read.close(); ?????} ?}?【運行結果】:hello
前面列舉的輸出輸入都是以文件進行的,現在我們以內容為輸出輸入目的地,使用內存操作流
ByteArrayInputStream 主要將內容寫入內容
ByteArrayOutputStream 主要將內容從內存輸出
使用內存操作流將一個大寫字母轉化為小寫字母
/** ??*?使用內存操作流將一個大寫字母轉化為小寫字母 ??*?*/?import?java.io.*; ?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????String?str="ROLLENHOLT"; ?????????ByteArrayInputStream?input=new?ByteArrayInputStream(str.getBytes()); ?????????ByteArrayOutputStream?output=new?ByteArrayOutputStream(); ?????????int?temp=0; ?????????while((temp=input.read())!=-1){ ?????????????char?ch=(char)temp; ?????????????output.write(Character.toLowerCase(ch)); ?????????} ?????????String?outStr=output.toString(); ?????????input.close(); ?????????output.close(); ?????????System.out.println(outStr); ?????} ?}?【運行結果】:
rollenholt
內容操作流一般使用來生成一些臨時信息采用的,這樣可以避免刪除的麻煩。
管道流
管道流主要可以進行兩個線程之間的通信。
PipedOutputStream 管道輸出流
PipedInputStream 管道輸入流
驗證管道流
/** ??*?驗證管道流 ??*?*/?import?java.io.*; ??/** ??*?消息發送類 ??*?*/?class?Send?implements?Runnable{ ?????private?PipedOutputStream?out=null; ?????public?Send()?{ ?????????out=new?PipedOutputStream(); ?????} ?????public?PipedOutputStream?getOut(){ ?????????return?this.out; ?????} ?????public?void?run(){ ?????????String?message="hello?,?Rollen"; ?????????try{ ?????????????out.write(message.getBytes()); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????}try{ ?????????????out.close(); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????} ?} ??/** ??*?接受消息類 ??*?*/?class?Recive?implements?Runnable{ ?????private?PipedInputStream?input=null; ?????public?Recive(){ ?????????this.input=new?PipedInputStream(); ?????} ?????public?PipedInputStream?getInput(){ ?????????return?this.input; ?????} ?????public?void?run(){ ?????????byte[]?b=new?byte[1000]; ?????????int?len=0; ?????????try{ ?????????????len=this.input.read(b); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????}try{ ?????????????input.close(); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????????System.out.println("接受的內容為?"+(new?String(b,0,len))); ?????} ?} ?/** ??*?測試類 ??*?*/?class?hello{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????Send?send=new?Send(); ?????????Recive?recive=new?Recive(); ?????????try{ ?//管道連接 ?????????????send.getOut().connect(recive.getInput()); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????????new?Thread(send).start(); ?????????new?Thread(recive).start(); ?????} ?}?【運行結果】:
?
接受的內容為 hello , Rollen
打印流
/** ??*?使用PrintStream進行輸出 ??*?*/?import?java.io.*; ??class?hello?{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????PrintStream?print?=?new?PrintStream(new?FileOutputStream(new?File("d:"?????????????????+?File.separator?+?"hello.txt"))); ?????????print.println(true); ?????????print.println("Rollen"); ?????????print.close(); ?????} ?}?【運行結果】:
true
Rollen
當然也可以格式化輸出
/** ??*?使用PrintStream進行輸出 ??*?并進行格式化 ??*?*/?import?java.io.*; ?class?hello?{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????PrintStream?print?=?new?PrintStream(new?FileOutputStream(new?File("d:"?????????????????+?File.separator?+?"hello.txt"))); ?????????String?name="Rollen"; ?????????int?age=20; ?????????print.printf("姓名:%s.?年齡:%d.",name,age); ?????????print.close(); ?????} ?}?【運行結果】:
姓名:Rollen. 年齡:20.
使用OutputStream向屏幕上輸出內容
/** ??*?使用OutputStream向屏幕上輸出內容? ??*?*/?import?java.io.*; ?class?hello?{ ?????public?static?void?main(String[]?args)?throws?IOException?{ ?????????OutputStream?out=System.out; ?????????try{ ?????????????out.write("hello".getBytes()); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????????try{ ?????????????out.close(); ?????????}catch?(Exception?e)?{ ?????????????e.printStackTrace(); ?????????} ?????} ?}?【運行結果】:
hello
輸入輸出重定向
import?java.io.File; ?import?java.io.FileNotFoundException; ?import?java.io.FileOutputStream; ?import?java.io.PrintStream; ??/** ??*?為System.out.println()重定向輸出 ??*?*/?public?class?systemDemo{ ?????public?static?void?main(String[]?args){ ?????????//?此刻直接輸出到屏幕 ?????????System.out.println("hello"); ?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt"); ?????????try{ ?????????????System.setOut(new?PrintStream(new?FileOutputStream(file))); ?????????}catch(FileNotFoundException?e){ ?????????????e.printStackTrace(); ?????????} ?????????System.out.println("這些內容在文件中才能看到哦!"); ?????} ?}?【運行結果】:
eclipse的控制臺輸出的是hello。然后當我們查看d盤下面的hello.txt文件的時候,會在里面看到:這些內容在文件中才能看到哦!
import?java.io.File; ?import?java.io.FileNotFoundException; ?import?java.io.FileOutputStream; ?import?java.io.PrintStream; ??/** ??*?System.err重定向?這個例子也提示我們可以使用這種方法保存錯誤信息 ??*?*/?public?class?systemErr{ ?????public?static?void?main(String[]?args){ ?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt"); ?????????System.err.println("這些在控制臺輸出"); ?????????try{ ?????????????System.setErr(new?PrintStream(new?FileOutputStream(file))); ?????????}catch(FileNotFoundException?e){ ?????????????e.printStackTrace(); ?????????} ?????????System.err.println("這些在文件中才能看到哦!"); ?????} ?}?【運行結果】:
你會在eclipse的控制臺看到紅色的輸出:“這些在控制臺輸出”,然后在d盤下面的hello.txt中會看到:這些在文件中才能看到哦!
import?java.io.File; ?import?java.io.FileInputStream; ?import?java.io.FileNotFoundException; ?import?java.io.IOException; ??/** ??*?System.in重定向 ??*?*/?public?class?systemIn{ ?????public?static?void?main(String[]?args){ ?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt"); ?????????if(!file.exists()){ ?????????????return; ?????????}else{ ?????????????try{ ?????????????????System.setIn(new?FileInputStream(file)); ?????????????}catch(FileNotFoundException?e){ ?????????????????e.printStackTrace(); ?????????????} ?????????????byte[]?bytes?=?new?byte[1024]; ?????????????int?len?=?0; ?????????????try{ ?????????????????len?=?System.in.read(bytes); ?????????????}catch(IOException?e){ ?????????????????e.printStackTrace(); ?????????????} ?????????????System.out.println("讀入的內容為:"?+?new?String(bytes,?0,?len)); ?????????} ?????} ?}?【運行結果】:
前提是我的d盤下面的hello.txt中的內容是:“這些文件中的內容哦!”,然后運行程序,輸出的結果為:讀入的內容為:這些文件中的內容哦!
轉自:http://zhaohe162.blog.163.com/blog/static/3821679720118309710914/
轉載于:https://www.cnblogs.com/hnrainll/archive/2012/01/18/2325844.html
總結
以上是生活随笔為你收集整理的Java中的IO整理完整版(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。