Bifalgorithm post · Computer science one · File Output
How can you write binary data to an output file?
How can you write binary data to an output file? (Computer science one · File Output) — "Just as with binary input, it is best to use a class that abstracts th…
Ideas in this flowchart
- Binary output should abstract file details, using FileOutputStream only when necessary.
- A class that abstracts the file type and data avoids low-level binary details.
- FileOutputStream can be used when raw binary output is required.
- Data can typically be loaded into a byte array before writing.
- The byte array can be dumped to the output file all at once.
- Try-with-resources automatically closes the FileOutputStream.
- An IOException can be caught and wrapped in a RuntimeException.
- Wrapping FileOutputStream in BufferedWriter will likely provide no performance benefit.
- Buffered output is preferable when writes are frequent and smaller than the buffer.
- Several individual integer values should be buffered and written together.
- The example writes a large amount of data, likely more than the typical 8KB buffer, at once.
- Using BufferedWriter would remove the ability to write raw byte data.