Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java FilterInputStream Class
2.1.
Declaration
2.1.1.
Code 1
3.
Java FilterInputStream Class Methods
4.
Java FilterOutputStream
4.1.
Declaration
4.1.1.
Code 2
5.
Java FilterOutputStream Class Methods
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

FilterInput/OutputStream

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In this article, we will be covering the FilterInput/OutputStream. However, before moving to the actual topic, we need to revise some previous concepts, which might help. Java Input/Output is a robust operation that provides all the input and output operations. A stream is a logical connection between a Java program and a file. The byte stream is usually used to perform an output and input function on 8-bits bytes.

Must Read, Multithreading in java and Hashcode Method in Java

Java FilterInputStream Class

Java FilterInputStream class contains different subclasses such as BufferedInputStream, DataInputStream to provide additional functionality. This class implements the InputStream. This is used occasionally.

Declaration

public class FilterInputStream extends InputStream

Content of input.txt

Hello This is File Input/Output Filter

Code 1

import java.io.*;
public class InputFilter1 {
	public static void main(String[] args) throws IOException {  
		File data = new File("input.txt");  
		FileInputStream file = new FileInputStream(data);  
		FilterInputStream filter = new BufferedInputStream(file);  
		int k =0;  
		while((k=filter.read())!=-1){  
			System.out.print((char)k);  
		}  
		file.close();  
		filter.close();  
	}  
}  

Output

Hello This is File Input/Output Filter

In this example, we have taken the use of Filter Input Stream. We have taken a file as an input, and we have printed the file's content. 

Practice by yourself on java online compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java FilterInputStream Class Methods


Also see,  Swap Function in Java

Java FilterOutputStream

Java FilterOutputStream class contains different subclasses such as BufferedOutputStream, DataOutputStream to provide additional functionality. This class implements the OutputStream. This is occasionally used.

Declaration

public class FilterOutputStream extends OutputStream

Code 2

import java.io.*;
public class OutputFilter1 {
	public static void main(String[] args) throws IOException {  
		File data = new File("input.txt");  
		FileOutputStream file = new FileOutputStream(data);  
		FilterOutputStream filter = new FilterOutputStream(file);  
		String s="Welcome to Coding Ninjas.";      
		byte b[]=s.getBytes();      
		filter.write(b);     
		filter.flush();  
		filter.close();  
		file.close();  
		System.out.println("Success...");  
	}  
}  

Output

Success...

In this example, we have used filter Output Steam; here, we must take a file as an input and check certain conditions. We also used different in-built methods, and after checking it, we displayed the message.

Java FilterOutputStream Class Methods

FAQs

  1. What is the standard input stream?
    When a program reads some files, it consumes them. Once it has been read, it cannot be backed up and reread.
     
  2. What is the difference between InputStream and OutputStream?
    The InputStream class defines the methods to perform input functions, whereas the OutputStream methods include methods designed to perform the task.
     
  3. Is there a limit in the size of the input stream?
    There is no restriction on the size of the input stream. 

Key Takeaways

In this article, we have covered Filter Input/Output Stream. We have briefly introduced the topic. We also discussed the Java Filter input Stream class with its declaration, example, and different methods. We also discussed the Filter output stream along with its declaration, example, and different methods. 

We hope this blog might help you a lot and help you to clear concepts. If you want to learn more coding articles, visit here Programming Fundamentals and solve coding problems, visit here Striver SDE Sheet Problem.

Previous article
Java Thread Dump Analyser
Next article
Sequence Input Stream
Live masterclass