如果您使用FileOutputStream
方法,则每次通过此方法编写文件时,您都会丢失旧数据。是否可以通过FileOutputStream
编写文件而不会丢失旧数据?
答案 0 :(得分:136)
使用带有File
和boolean
FileOutputStream(File file, boolean append)
并将布尔值设置为true
。这样,您编写的数据将附加到文件的末尾,而不是覆盖已存在的数据。
答案 1 :(得分:19)
使用构造函数将材料附加到文件中:
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);