The FileOutputStream class in Java is used to write byte data to a file. In this chapter, we will learn what the FileOutputStream class is, when to use it, its methods, and how to write data to a file using simple examples.
The FileOutputStream class is an output stream used to write data into a file. It writes data in the form of bytes and is mainly used for byte-oriented file operations.
If you want to write primitive or binary data into a file, the FileOutputStream class is suitable. Although it can also write character data, it is recommended to use the FileWriter class for character-oriented operations.
The following declaration shows how the FileOutputStream class is defined:
The FileOutputStream class provides constructors to create an output stream and connect it to a file.
This constructor creates a file output stream to write data to a file with the specified name. If the file already exists, its content is overwritten. If the file does not exist, a new file is created.
Syntax:
Here is the syntax of this constructor:
This constructor creates a file output stream to write data to a file. If append is set to true, the data is added at the end of the file. If it is false, the existing file content is overwritten.
Syntax:
Here is the syntax of this constructor:
This constructor creates a file output stream using a File object instead of a file name.
Syntax:
Here is the syntax of this constructor:
This constructor creates a file output stream using a File object and allows appending data to the file if required.
Syntax:
Here is the syntax of this constructor:
The following example demonstrates how to append data to an existing file using the FileOutputStream constructor.
Output:
data appended successfully
The FileOutputStream class provides several methods to write data to a file.
| Method | Description |
|---|---|
| protected void finalize() | It is used to clean up the connection with the file output stream. |
| void write(byte[] ary) | It is used to write ary.length bytes from the byte array to the file output stream. |
| void write(byte[] ary, int off, int len) | It is used to write len bytes from the byte array starting at offset off to the file output stream. |
| void write(int b) | It is used to write the specified byte to the file output stream. |
| FileChannel getChannel() | It is used to return the file channel object associated with the file output stream. |
| FileDescriptor getFD() | It is used to return the file descriptor associated with the stream. |
| void close() | It is used to closes the file output stream. |
In this example, a single byte value is written to a file. The byte value 65 represents the character A.
Output:
Success...
The content of a text file testout.txt is set with the data A.
testout.txt
A
In this example, a string is converted into a byte array and then written to a file using the FileOutputStream class.
Output:
Success...
The content of a text file testout.txt is set with the data Welcome to javaTpoint.
testout.txt
Welcome to javaTpoint.
We request you to subscribe our newsletter for upcoming updates.