# How to write to a file using FileOutputStream in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 6, 2019, updated Dec 7, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-write-file-using-fileoutputstream

> A short tutorial to learn how to write to a file using FileOutputStream in Java.

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:

```java
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:

```bash
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](https://attacomsian.com/blog/java-read-file-using-fileinputstream), 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:

```java
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:

```java
// 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:

```java
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:

```java
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:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
