import java.io.*;
public class CreateTxt{
public static void main(String args[])throws IOException{
File newTxt = new File("test.txt");
if( !newTxt.exists() ){
//在此java檔目錄下建立test.txt檔
newTxt.createNewFile();
}
else{
System.out.println("檔案已存在!");
}
}
}
寫入文字檔案
import java.io.*;
public class WriteTxt{
public static void main(String args[])throws IOException{
FileWriter dataFile = new FileWriter("test.txt");
BufferedWriter input = new BufferedWriter(dataFile);
//輸入Hello! World! 到test.txt檔裡
input.write("Hello! World!");
//寫入完關閉檔案,並儲存
input.close();
}
}
讀取文字檔案
import java.io.*;
public class ReadTxt{
public static void main(String args[])throws IOException{
FileReader dataFile = new FileReader("test.txt");
BufferedReader output = new BufferedReader(dataFile);
String str;
while( (str = output.readLine()) != null ){
System.out.println(str);
}
output.close();
}
}
謝謝板大^_^
回覆刪除謝謝板大^_^
回覆刪除