English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java FileOutputStream and its methods with the help of examples.
The java.io package's FileOutputStream class can be used to write data (in bytes) to a file.
It inherits the OutputStream abstract class.
Before learning FileOutputStream, make sure you understandJava File.
To create a file output stream, we must first import the java.io.FileOutputStream package. After importing the package, we can use Java to create a file output stream.
1Using the file path
//With boolean parameter FileOutputStream output = new FileOutputStream(String path, boolean value); //Without boolean parameter FileOutputStream output = new FileOutputStream(String path);
Here, we create an output stream that will be linked to the specified file path.
Additionally, the value is an optional boolean parameter. If it is set to true, new data will be appended to the end of the existing data in the file. Otherwise, new data will overwrite the existing data in the file.
2Using the file object
FileOutputStream output = new FileOutputStream(File fileObject);
Here, we create an output stream that will be linked to the file specified by fileObject.
The FileOutputStream class provides implementations for different methods that appear in the OutputStream class.
write() - Write a single byte to the byte file output stream
write(byte[] array) - Write the specified bytes to the output stream
write(byte[] array, int start, int length)-Write an equal number of bytes from the start position to the array's output stream
import java.io.FileOutputStream; public class Main { public static void main(String[] args) { String data = "This is a line of text in the file."; try { FileOutputStream output = new FileOutputStream("output.txt"); byte[] array = data.getBytes(); //Write bytes to the file output.write(array); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
In the above example, we created a file output stream named output. The file output stream is associated with the fileoutput.txtLink.
FileOutputStream output = new FileOutputStream("output.txt");
To write data to the file, we use the write() method.
Here, when we run the program,output.txtThe file will write the following content.
This is a line of text in the file.
Note:The getBytes() method is used in the program to convert a string to a byte array.
To clear the output stream, you can use the flush() method. This method forces the output stream to write all data to the target. For example,
import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { FileOutputStream out = null; String data = "This is a demonstration of the flush method"; try { out = new FileOutputStream("flush.txt"); //Use the write() method out.write(data.getBytes()); //Use the flush() method out.flush(); out.close(); } catch(Exception e) { e.getStackTrace(); } } }
When we run the program, the fileflush.txtFilled with text data represented by string
To close the file output stream, you can use the close() method. Once this method is called, we cannot use the methods of FileOutputStream.
Method | Content Description |
---|---|
finalize() | Ensure that the close() method is called |
getChannel() | Returns the object associated with the FileChannel and the output stream |
getFD() | Returns the file descriptor associated with the output stream |