Java writes data to a file without erasing old content

how can i write data to a file without erasing old content

+3
source share
3 answers

Use new FileOutputStream(file, true). This will open the file in "append" mode, which will add all the data written to the stream at the end of this file.

+5
source

Do you mean "how do you add to a file"? Find [append-version of constructor] [1] of your file writer class, for example:

public FileWriter(String fileName,
                  boolean append)
           throws IOException

Use this constructor and pass truein the append parameter.

[1]: http://download.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter(java.io.File , boolean)

+4

Java 7,

try (BufferedWriter writer = Files.newBufferedWriter(outFile, StandardCharsets.UTF_8, StandardOpenOption.APPEND))
0

Source: https://habr.com/ru/post/1782608/


All Articles