Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: FileOutputStream

Jakob Jenkov
Last update: 2014-11-15

The FileOutputStream class makes it possible to write a file as a stream of bytes. The FileOutputStream class is a subclass of OutputStream meaning you can use a FileOutputStream as an OutputStream.

FileOutputStream Example

Here is a simple FileOutputStream example:

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt");

while(moreData) {
  int data = getMoreData();
  output.write(data);
}
output.close();

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

FileOutputStream Constructors

The FileOutputStream class contains a set of different useful constructors. I will cover the most commonly used constructors here.

The first constructor takes a String which contains the path of the file to write to. Here is an example:

String path = "C:\\users\\jakobjenkov\\data\\datafile.txt";

FileOutputStream output = new FileOutputStream(path);

Notice the path String. It needs double backslashes (\\) to create a single backslash in the String, because backslash is an escape character in Java Strings. To get a single backslash you need to use the escape sequence \\.

On unix the file path could have looked like this:

String path = "/home/jakobjenkov/data/datafile.txt";

The second FileOutputStream constructor takes a File object which points to the file in the file system. Here is an example:

String path = "C:\\users\\jakobjenkov\\data\\datafile.txt";
File   file = new File(path);

FileOutputStream output = new FileOutputStream(file);

Overwriting vs. Appending the File

When you create a FileOutputStream pointing to a file that already exists, you can decide if you want to overwrite the existing file, or if you want to append to the existing file. You decide that based on which of the FileOutputStream constructors you choose to use.

This constructor which takes just one parameter, the file name, will overwrite any existing file:

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt");

There is a constructor that takes 2 parameters too: The file name and a boolean. The boolean indicates whether to append or overwrite an existing file. Here are two examples:

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt", true); //appends to file

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt", false); //overwrites file

write()

The write() method of a FileOutputStream takes an int which contains the byte value of the byte to write.

The FileOutputStream has other constructors too, letting you specify the file to write to in different ways. Look in the official JavaDoc for more detailed info.

Writing Byte Arrays

Since the FileOutputStream is a subclass of OutputStream, you can write arrays of bytes to the FileOutputStream too, instead of just a single byte at a time. See my tutorial about the OutputStream class for more information about how to write arrays of bytes.

flush()

When you write data to a FileOutputStream the data may get cached internally in the memory of the computer and written to disk at a later time. For instance, every time there is X amount of data to write, or when the FileOutputStream is closed.

If you want to make sure that all written data is written to disk without having to close the FileOutputStream you can call its flush() method. Calling flush() will make sure that all data which has been written to the FileOutputStream so far, is fully written to disk too.

close()

Like any other OutputStream a FileOutputStream instance needs to be closed after use. You do so by calling its close() method as shown in the example earlier in this text.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC