如何使用FileOutputStream写入数据而不丢失旧数据?

iSu*_*Sun 82 java fileoutputstream

如果使用FileOutputStream方法,每次通过这种方法编写文件时,都会丢失旧数据.是否可以通过写入文件而不丢失旧数据FileOutputStream

Mat*_*Mat 141

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

FileOutputStream(File file, boolean append) 
Run Code Online (Sandbox Code Playgroud)

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


o_o*_*o_o 19

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

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

所以要附加到文件中说"abc.txt"使用

FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true);
Run Code Online (Sandbox Code Playgroud)