如何在不丢失旧数据的情况下使用 FileOutputStream 写入数据?

如果使用 FileOutputStream方法,每次通过这种方法写文件时,都会丢失旧数据。有没有可能写文件而不丢失你的旧数据通过 FileOutputStream

123008 次浏览

Use the constructor that takes a File and a boolean

FileOutputStream(File file, boolean append)

and set the boolean to true. That way, the data you write will be appended to the end of the file, rather than overwriting what was already there.

Use the constructor for appending material to the file:

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

So to append to a file say "abc.txt" use

FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true);