Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FilterReader markSupported() Method

In this tutorial, we will learn about the markSupported() method of FilterReader class in Java. This method verifies, whether this FileReader stream, supports the operation of mark() method or not. It is a non-static method, available in the java.io package. It can only be accessed using class objects, as trying to access it with the class name, will throw an IOException.

Syntax

This is the syntax declaration of this method. No parameter is required. The return type of this method is boolean, which means it returns true if the mark() method is supported, otherwise, it returns false.

public boolean markSupported()

Example 1: Mark Support in FilterReader

The markSupported() method is used to check if the current buffer is supporting the mark() method or not, in the current example firstly we check if this method supports the mark() method then we set the mark at the current position in a stream and this way we avoid the possible errors.

import java.io.FileReader;
import java.io.FilterReader;
import java.io.Reader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new FileReader("E:\\studytonight\\output.txt");  
			FilterReader filterReader = new FilterReader(reader) {};  
			int i;  
			while ((i = filterReader.read()) != -1) {  
				System.out.print((char) i);  
			}  
			filterReader.close();  
			reader.close();  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}

Hello Studytonight

output.txt

Hello Studytonight

Example 1: Mark Support in FilterReader

This is the simplified version of the above program here we are checking if the current buffer supports the mark() method then the markSupported() method will return true value otherwise it will return a false value.

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(); 
		}
		catch(Exception e)
		{
			System.out.print("Error: "+e.toString());
		}
	} 
}


The Stream Supports the mark() method: true

Conclusion

In this tutorial, we learned about the markSupported() method of the FilterReader class in Java, which is used to check whether the current stream supports the mark() method or not. If yes, it returns true, else, false.