Java FileOutputStream Class

Java FileOutputStream is an output stream used for writing data to a file.

If you have to write primitive values into a file, use FileOutputStream class. You can write byte-oriented as well as character-oriented data through FileOutputStream class. But, for character-oriented data, it is preferred to use FileWriter than FileOutputStream.


FileOutputStream class declaration

Let's see the declaration for Java.io.FileOutputStream class:


FileOutputStream class methods

MethodDescription
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.

Java FileOutputStream Example 1: write byte

Output:

Success...

The content of a text file testout.txt is set with the data A.

testout.txt

A

Java FileOutputStream example 2: write string

Output:

Success...

The content of a text file testout.txt is set with the data Welcome to javaTpoint.

testout.txt

Welcome to javaTpoint.




Latest Courses