Signup/Sign In

Java FileOutputStream Class

In this tutorial, we will learn about FileOutputStream in Java. This class is an output stream that is used to write the data in files.

Syntax

This is the syntax declaration of FileOutputStream class, we can see this class inherited the OutputStream it means we can call all the method of this class using an object of FileOutputStream class.

public class FileOuputStream extends OutputStream  

Constructors

There are different constructors given in the table below:

Constructor Description
FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj) Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(String name) Creates a file output stream to write to the file with the specified name.
FileOutputStream(String name, boolean append) Creates a file output stream to write to the file with the specified name.

Methods

These are the supported methods of the FileOutputStream Class.

Method Description
protected void finalize() This method is used to clean up the connection with the file output stream.
void write(byte[] arr) This method is used to write ary.length bytes from the byte array to the file output stream.
void write(byte[] arr, int off, int len) This method is used to write len bytes from the byte array starting at an offset off to the file output stream.
void write(int b) This method is used to write the specified byte to the file output stream.
FileChannel getChannel() This method is used to return the file channel object associated with the file output stream.
FileDescriptor getFD() This method is used to return the file descriptor associated with the stream.
void close() This method is used to closes the file output stream.

Example of write() method:

In the following program, we converted the given text to an array of bytes because write() method accepts only an array of bytes. To write this array of bytes we need an object of FileOutputStream class and we call write() method on this object to write into the file.

import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		try
		{    
			byte arr[] = "Hello studytonight".getBytes();
			FileOutputStream fout=new FileOutputStream("E:\\studytonight\\myfile.txt");    
			fout.write(arr);    
			fout.close();    
			System.out.println("Text written to the file successfully.");    
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}    
	}  
}

Console Output:


Text written to the file successfully.

File Output:


Hello studytonight

Example of FileOutputStream constructor for appending:

The example is given in below we are implementing FileOutputStream with two parameters 1)string value of filename, 2)boolean value for append. This example will append the given text to the file each time we execute the program.

import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		try
		{    
			//second parameter is for append
			FileOutputStream fout=new FileOutputStream("E:\\studytonight\\myfile.txt", true);    
			byte arr[] = "Hello studytonight\n".getBytes();
			fout.write(arr);    
			fout.close();    
			System.out.println("Text written to the file successfully.");    
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}    
	}  
}


Text written to the file successfully.

When we execute the above program three times then the data inside myfile.txt will look like this:

Hello studytonight
Hello studytonight
Hello studytonight

mailConclusion

In this article, we learned how to use write() method of OutputStream class to write data into files. This method writes bytes from the byte array to the file output stream.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.