In this quick article, you'll learn how to write to a file using the FileOutputStream class in Java. FileOutputStream is a bytes stream class that can be used to write streams of raw bytes to a binary file.

Using FileOutputStream Class

The following example shows how you can convert data to bytes and then use the FileOutputStream class to write it to a file:

try {
    // create a writer
    FileOutputStream fos = new FileOutputStream(new File("output.txt"));

    // write data to file
    fos.write("Hey, there!".getBytes());
    fos.write("\n".getBytes());
    fos.write("How are you doing?".getBytes());

    // close the writer
    fos.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

The above code will create a new file called output.txt with the following contents:

Hey, there!
How are you doing?

The above code performs an I/O write operation to the disk every time the write() method is called. It will run slower for large-sized files.

To better performance, you should use the BufferedOutputStream class instead. Just like BufferedInputStream, it uses an internal buffer of size 8KB (can be customized) to store the data and only writes to the disk when the buffer is full.

Here is an example:

try {
    // create a writer
    FileOutputStream fos = new FileOutputStream(new File("output.txt"));
    BufferedOutputStream writer = new BufferedOutputStream(fos);

    // write data to file
    writer.write("Hey, there!".getBytes());
    writer.write("\n".getBytes());
    writer.write("How are you doing?".getBytes());

    // flush remaining bytes
    writer.flush();
    
    // close the writer
    writer.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

To specify a custom internal buffer size, do the following:

// custom buffer size
int BUFFER_SIZE = 32 * 1024; // 32KB

// create a writer
FileOutputStream fos = new FileOutputStream(new File("output.txt"));
BufferedOutputStream writer = new BufferedOutputStream(fos, BUFFER_SIZE);

To write to a file with different character encoding scheme, you should use the OutputStreamWriter class to wrap the FileOutputStream object:

try {
    // create a writer
    FileOutputStream fos = new FileOutputStream(new File("output.txt"));

    // set encoding to UTF_8
    OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8);

    // write data to file
    writer.write("Hey, there!");
    writer.write("\n");
    writer.write("How are you doing?");

    // flush remaining bytes
    writer.flush();

    // close the writer
    writer.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Using Files.newOutputStream() Method

If you are using Java 8 or higher, you can the Files.newOutputStream() static method to initialize a stream as shown below:

try {
    // create a writer
    OutputStream os = Files.newOutputStream(Paths.get("output.txt"));

    // write data to file
    os.write("Hey, there!".getBytes());
    os.write("\n".getBytes());
    os.write("How are you doing?".getBytes());

    // close the writer
    os.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.