我有一個應用程序可以創建一個.txt文件。我想覆蓋它。這是我的功能:如何覆蓋現有的.txt文件
try{
String test = "Test string !";
File file = new File("src\\homeautomation\\data\\RoomData.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}else{
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(test);
bw.close();
System.out.println("Done");
}catch(IOException e){
e.printStackTrace();
}
我應該在else子句中放置什麼,如果文件存在,那麼它可以被覆蓋?
[改寫txt文件在Java]的可能的複製(http://stackoverflow.com/questions/13729625/overwrite-txt-file-in-java) – Galabyca 2016-03-08 15:25:09