Junior — Middle
Does using FileOutputStream replace existing data in the file when writing?
sobes.tech AI
Answer from AI
Yes, when using the FileOutputStream constructor without additional parameters, the file is opened for writing with overwrite, meaning existing data in the file will be replaced with new data.
Example:
try (FileOutputStream fos = new FileOutputStream("file.txt")) {
String data = "New data";
fos.write(data.getBytes());
}
If you want to append data to the end of the file, you should use the constructor with the append parameter:
FileOutputStream fos = new FileOutputStream("file.txt", true); // true for appending
Thus, by default, the file content is replaced.