Java FileOutputStream
Introduction to FileOutputStream
In previous sections, you learned how to write text to files using the FileWriter class. While
FileWriter is convenient for writing character-based text, Java also provides another powerful
class for handling file output at a lower level: FileOutputStream.
FileOutputStream writes data as raw bytes rather than characters. Because of this, it can be
used to write both text and binary files, including images, PDFs, audio files, and other non-text
formats.
This class is commonly used when developers need precise control over how data is written to
a file.
Writing Text to a File Using FileOutputStream
Although FileOutputStream is designed for byte-based operations, it can also write text by
converting a string into bytes.
The following example demonstrates how to write a simple message into a file.
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteFileWriter {
public static void main(String[] args) {
String message = "Java file output using byte streams.";
try (FileOutputStream stream = new FileOutputStream("output.txt")) {
byte[] data = message.getBytes();
stream.write(data);
System.out.println("Text written to file successfully.");
} catch (IOException error) {
System.out.println("Failed to write data to the file.");
error.printStackTrace();
}
}
}
Sample Output
Text written to file successfully.
Explanation
- The program creates a string named message.
- The getBytes() method converts the string into a byte array.
- FileOutputStream writes these bytes to the file output.txt.
- If the file already exists, its contents are overwritten.
- The try-with-resources statement ensures the output stream is automatically closed.
Real-World Example: Copying a Binary File
One of the most common uses of FileOutputStream is writing binary data when copying files.
In this example, a document file is copied from one location to another using both
FileInputStream and FileOutputStream.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
try (FileInputStream source = new FileInputStream("document.pdf");
FileOutputStream destination = new FileOutputStream("document_backup.pdf")) {
int byteValue;
while ((byteValue = source.read()) != -1) {
destination.write(byteValue);
}
System.out.println("File copied successfully.");
} catch (IOException error) {
System.out.println("Error occurred during file copy.");
error.printStackTrace();
}
}
}
How This Example Works
- The program reads data from document.pdf using FileInputStream.
- Each byte is written to document_backup.pdf using FileOutputStream.
- Because the program processes raw bytes, it works for any type of file, including images, videos, or documents.
Appending Data to an Existing File
By default, FileOutputStream overwrites existing file content. If you want to add new data
without deleting the current content, you must enable append mode.
This can be done by passing true as the second parameter to the constructor.
import java.io.FileOutputStream;
import java.io.IOException;
public class AppendDataExample {
public static void main(String[] args) {
String newEntry = "\nLog entry: Application executed successfully.";
try (FileOutputStream stream = new FileOutputStream("output.txt", true)) {
stream.write(newEntry.getBytes());
System.out.println("Data appended to the file.");
} catch (IOException error) {
System.out.println("Unable to append data.");
error.printStackTrace();
}
}
}
Sample Output
Data appended to the file.
Explanation
In this example:
- The file output.txt is opened in append mode.
- The new text is added at the end of the existing file.
- If the file does not exist, Java automatically creates it.