Signup/Sign In

Java FilterReader close() Method

In this tutorial, we will learn about the close() method of FilterReader class in Java. This method closes the stream and frees all the system resources associated with it. Calling of this method invalidates invocation of read(), ready(), mark(), reset() or skip() methods. This non-static method is available in the java.io package and can only be accessed using class objects, trying to invoke it by class name results in an error.

Syntax

This is the syntax declaration of this method, which does not take any parameter or return any value.

public void close()

Example 1: Close FilterReader in Java

In this example, we have created a FilterReader stream to read the data, in this particular case we are checking if the stream supports the mark() method or not, once the operations related to the stream are completed we call the close() method to free up all the resources linked or currently allocated for the stream, after this point we cannot use this stream.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Hello Studeytonight"); 
			FilterReader fileReader = new FilterReader(reader){};
		
			boolean isSupported = fileReader.markSupported();			
			System.out.println("The Stream Supports the mark() method: "+isSupported);	
			
			fileReader.close(); 
			System.out.println("FilterReader is closed successfully...");			
		}
		catch(Exception e)
		{
			System.out.print("Error: "+e.toString());
		}
	} 
}


The Stream Supports the mark() method: true
FilterReader is closed successfully...

Example 1: Close FilterReader in Java

In this example, we are reading the data from the FIlterReader stream and once the reading is done we close this stream using the close() method to free up all the resources linked with the stream.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Hello Studeytonight"); 
			int i;
			FilterReader filterReader = new FilterReader(reader) {};

			while ((i = filterReader.read()) != -1) {
				char  c = (char) i;
				System.out.print(c);
			}

			filterReader.close(); 
			System.out.println("\nFileReader is closed successfully...");
		}
		catch(Exception e)
		{
			System.out.print("Error: "+e.toString());
		}
	} 
}


Hello Studeytonight
FileReader is closed successfully...

Conclusion

In this tutorial, we learned about the close() method of the FilterReader class in Java, which is used to close the stream and release all the resources linked to it. It is available in the java.io package and should be accessed only using class objects.



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.