1.以檔案流的方式複製檔案
將一個已存在檔案複製到指定位置,如本例中將源檔案"/home/hadoop/Scene1"拷貝一份重新命名為“/home/hadoop/Scene1bak”
代碼如下:
View Code
package erp.sjtu.xw;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class CopyFile { public static void main(String args[]) throws IOException { CopyFile cf=new CopyFile(); String src="/home/hadoop/Scene1"; String dest="/home/hadoop/Scene1bak"; cf.copyFile(src, dest); } /**以檔案流的方式複製檔案 * @param src 檔案來源目錄 * @param dest 檔案目的目錄 * @throws IOException * 該方法經過測試,支援中文處理,並且可以複製多種類型,比如txt,xml,jpg,doc等多種格式 */ public void copyFile(String src,String dest) throws IOException { FileInputStream in=new FileInputStream(src);//定義輸入資料流 File file=new File(dest);//定義檔案,指定位置"dest"可能已經存在了該檔案,如果不存在需要建立。 if(!file.exists())//如果檔案不存在,則調用建立檔案的方法 file.createNewFile(); FileOutputStream out=new FileOutputStream(file);//定義輸出資料流 int c; byte buffer[]=new byte[1024];//定義緩衝大小。 /** * 將輸入檔案中的常值內容讀入緩衝buffer中去。 * 如果文字大小小於buffer的容量,那麼讀一次就可以。 * 如果大於buffer大小,則需要多次讀取。 */ while((c=in.read(buffer))!=-1)//從輸入資料流中讀取常值內容讀入buffer,知道讀完為止 { System.out.println(c);//輸出buffer中讀取的文本長度 for(int i=0;i<c;i++) out.write(buffer[i]);//將buffer中的內容吸入輸出資料流。 } in.close(); out.close(); }}
輸出結果為:
1024
89
而Scene1檔案大小也剛好為(1113=1024+89)位元組2.獲得控制台使用者輸入的資訊
這個程式使用者獲得控制台輸入的資訊,代碼如下:
View Code
package erp.sjtu.xw;import java.io.IOException;public class GetConsoleInputMessage { public static void main(String args[]) throws IOException { GetConsoleInputMessage instance=new GetConsoleInputMessage(); String str=instance.getInputMessage(); System.out.println(str); System.exit(0); } /**獲得控制台使用者輸入的資訊 * @return * @throws IOException * 可以返回使用者輸入的資訊,不足之處在於不支援中文輸入,有待進一步改進。 */ public String getInputMessage() throws IOException{ String str=null; System.out.println("請輸入命令:"); byte buffer[]=new byte[1024];//建立緩衝 int count=System.in.read(buffer);//將console中的輸入寫入到緩衝buffer中,並返回緩衝字串長度。 System.out.println(count); //char ch[]=new char[count-2];//buffer中最後兩位是結束符,沒有意思,因此刪除。 char ch[]=new char[count-1];//定義一個字元數組,長度為count-1,因為buffer中最後一位是結束符,沒有意思,因此刪除。 for(int i=0;i<count-1;i++) { ch[i]=(char)buffer[i];// str=new String(ch);// System.out.println(str); } str=new String(ch);//將字元數組轉變成字串。 return str; }}
輸出結果:
請輸入命令:please input you commond25please input you commond
注意:這裡輸入輸出的buffer長度是25,但是實際上字串中有用的只有24個字元,這是因為buffer中最後一位是結束符。
3.利用PrintStream寫檔案
使用PrintStream能夠往空檔案中寫入文本,代碼如下:
View Code
package erp.sjtu.xw;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;public class PrintStreamDemo { public static void main(String args[]) throws IOException { PrintStreamDemo psd=new PrintStreamDemo(); psd.printStreamDemo();//test printStreamDemo() psd.printStreamDemo2();////test printStreamDemo2() } public void printStreamDemo() throws IOException { try{ String src="/home/hadoop/printStreamDemo";//源檔案路徑 File file=new File(src);//定義檔案,如果不存在路徑所指定的檔案則建立 if(!file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file);//定義檔案輸出資料流 PrintStream ps=new PrintStream(out);//往檔案輸出資料流中列印內容 for(int i=0;i<10;i++) ps.println("This is "+i+" line."); } catch (FileNotFoundException e){ e.printStackTrace(); } } /** * 利用PrintStream寫檔案 */ public void printStreamDemo2() throws IOException { try{ /** * 註:FileOutputStream out=new FileOutputStream("/home/hadoop/printStreamDemo2"); * 如果本地目錄有檔案printStreamDemo3,則文本寫入該檔案中。 * 如果本地目錄中沒有printStreamDemo3這個檔案,會自動建立該檔案,並向其中寫入文本。 * 這是前面如下代碼的操作集合 * String src="/home/hadoop/printStreamDemo";//源檔案路徑 * File file=new File(src);//定義檔案,如果不存在路徑所指定的檔案則建立 * if(!file.exists()) * file.createNewFile(); * FileOutputStream out=new FileOutputStream(file);//定義檔案輸出資料流 */ FileOutputStream out=new FileOutputStream("/home/hadoop/printStreamDemo2"); PrintStream ps=new PrintStream(out); for(int i=0;i<10;i++) ps.println("This is "+i+" line."); } catch (FileNotFoundException e){ e.printStackTrace(); } }}
運行上述代碼,將自動產生printStreamDemo和printStreamDemo2這兩個檔案,這了這兩個檔案的常值內容都是:
This is 0 line.This is 1 line.This is 2 line.This is 3 line.This is 4 line.This is 5 line.This is 6 line.This is 7 line.This is 8 line.This is 9 line.
4. 利用StringBuffer寫檔案
代碼如下:
View Code
package erp.sjtu.xw;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class StringBufferDemo { public static void main(String args[]) throws IOException { StringBufferDemo sbd=new StringBufferDemo();//建立對象執行個體 sbd.stringBufferDemo();//調用方法,方法和類同名,有點彆扭 } /** * 利用StringBuffer寫檔案 * 該方法可以設定使用何種編碼,有效解決中文問題。 * @throws IOException */ public void stringBufferDemo() throws IOException { String src="/home/hadoop/StringBufferDemo"; File file=new File(src); if(file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file,true); for(int i=0;i<10;i++) { StringBuffer sb=new StringBuffer(); sb.append("這是第"+i+"行。/n");//如果不加"/n"則不能實現換行。 out.write(sb.toString().getBytes("utf-8")); } out.close(); }}
運行上述代碼將自動組建檔案StringBufferDemo,其常值內容為:
這是第0行。這是第1行。這是第2行。這是第3行。這是第4行。這是第5行。這是第6行。這是第7行。這是第8行。這是第9行。
PS:java 流(Stream)的概念(2011-10-13)
流是 Java 中最重要的基本概念之一。檔案讀寫、網路收發、進程通訊,幾乎所有需要輸入輸出的地方,都要用到流。流是做什麼用的呢?就是做輸入輸出用的。為什麼輸入輸出要用“流”這種方式呢?因為程式輸入輸出的基本單位是位元組,輸入就是擷取一串位元組,輸出就是發送一串位元組。但是很多情況下,程式不可能接收所有的位元組之後再進行處理,而是接收一點處理一點。比方你下載魔獸世界,不可能全部下載到記憶體裡再儲存到硬碟上,而是下載一點就儲存一點。這時,流這種方式就非常適合。在 Java 中,每個流都是一個對象。流分為兩種:輸入資料流(InputStream)和輸出資料流(OutputStream)。對於輸入資料流,你只要從流當中不停地把位元組取出來就是了;而對於輸出資料流,你只要把準備好的位元組串傳給它就行。
________Java 程式
| |
外部系統 --|--(輸入資料流)--> 處理邏輯 --(輸出資料流)---|--> 外部系統
|__________________________|
總體而言,我們要站在記憶體的角度想象java的輸入資料流與輸出資料流。輸入資料流就是“我們是記憶體,外部系統有內容要輸入到記憶體中,所以叫輸入資料流”。輸出資料流就是“我們是記憶體,我們要將資料從記憶體輸出,寫入到外部系統,所以叫輸出資料流。”