FileOutputStream is an output stream used for writing data to a File or a FileDescriptor. It is part of the java.io package and is a subclass of the abstract OutputStream. While FileWriter is for text, FileOutputStream is for bytes—making it the essential tool for handling images, audio, and compressed files.
When you initialize a FileOutputStream, Java establishes a direct connection to a file on the disk. If the file doesn't exist, Java will attempt to create it. If it does exist, Java will—by default—overwrite everything inside it. This is the rawest way to export data from the JVM.
This is the most critical decision when using this class. Do you want to start a fresh file, or add to the end of an existing one?
new FileOutputStream("data.bin");
If the file exists, its previous contents are deleted immediately upon opening the stream.
new FileOutputStream("data.bin", true);
The second boolean parameter true tells Java to keep the existing data and write new data at the end.
| Method | Functionality |
|---|---|
void write(int b) |
Writes a single byte. Note: The input is an int, but only 8 bits are written. |
void write(byte[] b) |
Writes the entire byte array to the stream. Much more efficient for large data. |
void flush() |
Forces any buffered output bytes to be written out to the file. |
void close() |
Closes the stream. This also performs an automatic flush(). |
Since FileOutputStream only understands bytes, you cannot simply pass a String to it. You must convert your text into a byte array first using the getBytes() method.
String message = "Hello Binary World!";byte[] data = message.getBytes(); // Converts text to byte datafos.write(data);
This program demonstrates how to write a string as bytes and how to properly use the append mode within a try-with-resources block.
Similar to reading, writing one byte at a time (write(int b)) is extremely taxing on the CPU and Hard Drive. Every time you call that method, the JVM has to ask the Operating System to access the disk.
To optimize this, we usually wrap FileOutputStream in a BufferedOutputStream. This collects data in a memory buffer and only writes to the disk when the buffer is full.
try-with-resources. An unclosed FileOutputStream can prevent other applications from accessing the file.AccessDeniedException.IOException mid-write.Q: What happens if I provide a path to a file that doesn't exist?
A: FileOutputStream will create a new file at that location. However, if the folder doesn't exist, it will throw a FileNotFoundException.
Q: Is FileOutputStream thread-safe?
A: No. If multiple threads write to the same FileOutputStream simultaneously, the data will likely become corrupted or interleaved. You must synchronize the access.
Q: When should I use FileOutputStream instead of FileWriter?
A: Use FileOutputStream for everything that isn't plain text (images, executable files, zip files). Even for text, if you need to be 100% sure about the exact byte-level output, FileOutputStream is safer.
The FileOutputStream is your gateway to data persistence. By mastering its append modes and understanding its byte-level nature, you can build applications that save complex states, generate media, and log system events with absolute precision. Just remember: in the world of bytes, order is everything.
Next: Speeding up I/O with Buffered Streams →