When working with FileOutputStream, writing to a file with the default settings overwrites existing data. To preserve previous content while writing, consider the following approach:
Using the Constructor with Append Option
The FileOutputStream class offers a constructor that accepts both a File and a boolean flag, append. Setting this flag to true ensures that written data is appended to the end of the file instead of replacing the old data.
<code class="java">FileOutputStream fos = new FileOutputStream(file, true);</code>
When you subsequently write to the file using this stream, the data will be added to the end of the file without affecting the existing content. As a result, using the append option allows for seamless appending of data to a file without the risk of losing it.
The above is the detailed content of How to Append Data to a FileOutputStream Without Overwriting Existing Content?. For more information, please follow other related articles on the PHP Chinese website!