我创建了一个临时文件,写入它,我想覆盖现有文件
- 创建临时文件
- 填补;填写(表格,资料
- 打开旧文件
- 将旧文件设置为等于新文件
这是我的代码,但它不起作用
如果您能找到问题,请告诉我。谢谢!
try{
//create a temporary file
File temporary=File.createTempFile("tmp", "");
BufferedWriter writer = new BufferedWriter(new FileWriter(temporary));
//Write each line to file (temporary)
for (String string : parsedArticlesToSave) {
writer.write (String.format("%s\n", string));
}
//load old file
File oldFile = new File("StringFile/ArticlesDB.txt");
//replace old file with new file
oldFile=temporary;
//release resources
writer.close();
}catch(Exception e){
e.printStackTrace();
}