Junior — Middle
Does the use of FileOutputStream provide the ability to append to an existing file when writing?
sobes.tech AI
Answer from AI
Yes, the FileOutputStream class in Java allows appending data to an existing file when using the appropriate constructor with the append parameter set to true.
Example:
try (FileOutputStream fos = new FileOutputStream("file.txt", true)) {
String data = "Additional line\n";
fos.write(data.getBytes());
}
In this example, data will be added to the end of the file.txt file without overwriting its contents. If the append parameter is not specified or set to false, the file will be overwritten.