90

如果您使用FileOutputStream方法,则每次通过此方法编写文件时,您都会丢失旧数据。是否可以在不丢失旧数据的情况下写入文件FileOutputStream

4

2 回答 2

166

File使用带有 a和 a的构造函数boolean

FileOutputStream(File file, boolean append) 

并将布尔值设置为true. 这样,您写入的数据将附加到文件的末尾,而不是覆盖已经存在的数据。

于 2011-12-17T12:36:41.107 回答
20

使用构造函数将材料附加到文件中:

FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.

所以要附加到一个文件说“abc.txt”使用

FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true);
于 2011-12-17T12:37:14.107 回答