JAVA-文字檔的寫入
文字檔既是.txt的文件,是可以在文字編輯器中閱讀和編輯的檔案.
import java.io.*; //檔案的輸入輸出都需要這個類別庫
public class out {
public static void main(String args[])
throws IOException{ //throws IOException 是必要的例外
String s="字串的輸入\r\n"; //宣告字串變數s并指派値,\r\n在windows系統文字本中表示換行
char c[] = {'e','n','d','i','n','g'}; //宣告陣列字元c并指派値
//建立FileWriter物件fw,和文子檔out.txt.true為布林値,既將資料增加到原有檔案,false則覆寫
FileWriter fw=new FileWriter("out.txt",true); //out.txt新增到java檔的根目錄
fw.write(s); //將字串s寫入檔案,s是先前的字串變數
fw.write(c); //將字元c寫入檔案,c是先前的字元陣列變數
fw.close(); //關閉檔案,既out.txt
}
}
留言
張貼留言