我有以下函数写入文件:
public void writeToFile(String data) throws IOException {
FileWriter fstream = null;
try {
fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(data);
//Close the output stream
out.close();
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fstream.close();
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
每次调用它时它会做什么创建一个新文件并写一个新行.我想要实现的是检查文件是否存在.如果没有创建新文件并写入内容(如果存在),则打开文件并添加新行而不删除现有文本.怎么做?
将FileOutputStream构造函数调用更改为:
fstream = new FileWriter("out.txt", true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2731 次 |
| 最近记录: |