Signup/Sign In

Java FilterOutputStream Class

In this tutorial, we will learn about FilterOutputStream class in Java. The write() method of FilterOutputStream class filters the data and writes it to the stream, filtering which is done based on the Streams.

Syntax

The internal syntax declaration of the FilterOutputStream class is given the below code snippet.

public class FilterOutputStream extends OutputStream  

Java FilterOutputStream class Methods

All the methods supported by FilterOutputStream class are given in the table below:

Method Description
void write(int b) This method is used to write the specified byte to the output stream.
void write(byte[] array) This method is used to write array.length byte to the output stream.
void write(byte[] b, int off, int len) This method is used to write len bytes from the offset off to the output stream.
void flush() This method is used to flushes the output stream.
void close() This method is used to close the output stream.

Example of FilterOutputStream class

In this program, we are writing data to the file output.txt using write() method of FilterOutputStream class.

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		File data = new File("E:\\studytonight\\output.txt");  
		FileOutputStream file = new FileOutputStream(data);  
		FilterOutputStream filter = new FilterOutputStream(file);  
		String s="Hello Studytonight";      
		byte b[]=s.getBytes();      
		filter.write(b);     
		filter.flush();  
		filter.close();  
		file.close();  
		System.out.println("Data is written to the file successfully...");  
	}  
}


Data is written to the file successfully...

output.txt

Hello Studytonight

Conclusion

In this tutorial, we learned about FilterOutputStream class and its various methods. This class filters the data and writes it to the stream, filtering which is done based on the Streams.



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.