Does FileOutputStream truncate an existing file
file, java
Solution
`FileOutputStream` without the `append` option does truncate the file.
Note that `FileOutputStream` opens a Stream, not a random access file, so i guess it does make sense that it behaves that way, although i agree that the documentation could be more explicit about it.
Problem
Does ``` final OutputStream output = new FileOutputStream(file); ``` truncate the `file` if it already exists? Surprisingly, the API documentation for Java 6 does not say. Nor does the API documentation for Java 7. The specification for the language itself has nothing to say about the semantics of the `FileOutputStream` class. I am aware that ``` final OutputStream output = new FileOutputStream(file, true); ``` causes appending to the file. But appending and truncating are not the only possibilities. If you write 100 bytes into a 1000 byte file, one possibility is that the final 900 bytes are left as they were.