
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - BufferedReader markSupported() method
Description
The Java BufferedInputStream markSupported() method returns true if the stream supports mark() method.
The markSupported() method is used to check if the current reader supports the mark() and reset() methods. It returns true if marking is supported, otherwise false.
Declaration
Following is the declaration for java.io.Bufferedreader.marksupported() method.
public boolean markSupported()
Parameters
NA
Return Value
The method returns boolean value. Returns true if mark() is supported.
Exception
NA
Assumption
Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −
ABCDE
Example - Using markSupported() method
The following example shows the usage of Java BufferedReader markSupported() method.
BufferedReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class BufferedReaderDemo { public static void main(String[] args) throws Exception { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try { // open input stream example.txt for reading purpose. is = new FileInputStream("example.txt"); // create new input stream reader isr = new InputStreamReader(is); // create new buffered reader br = new BufferedReader(isr); boolean bool = false; // returns true if the stream type supports mark bool = br.markSupported(); System.out.println("Buffered reader supports mark : "+bool); } catch(Exception e) { e.printStackTrace(); } finally { // releases resources associated with the streams if(is!=null) is.close(); if(isr!=null) isr.close(); if(br!=null) br.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Buffered reader supports mark : true
Example - Checking if mark() and reset() methods are Supported
The following example shows the usage of Java BufferedReader markSupported() method.
BufferedReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; public class BufferedReaderDemo { public static void main(String[] args) { // Input string String input = "Hello, World!\nThis is a BufferedReader example."; // Create a BufferedReader try (BufferedReader reader = new BufferedReader(new StringReader(input))) { // Check if mark and reset are supported if (reader.markSupported()) { System.out.println("Mark and reset are supported by this BufferedReader."); // Use mark and reset as they are supported reader.mark(100); // Mark the position System.out.println("First line: " + reader.readLine()); // Reset to the marked position reader.reset(); System.out.println("After reset: " + reader.readLine()); } else { System.out.println("Mark and reset are not supported by this BufferedReader."); } } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Mark and reset are supported by this BufferedReader. First line: Hello, World! After reset: Hello, World!
Explanation
The markSupported() method is called to check if the BufferedReader supports marking.
If supported, the mark() and reset() methods are used to demonstrate how to revisit the marked position.
If not supported, an appropriate message is displayed.
Example - Demonstrating markSupported() with an Unsupported Reader
The following example shows the usage of Java BufferedReader markSupported() method.
BufferedReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class BufferedReaderDemo { public static void main(String[] args) { // Custom reader that does not support mark Reader unsupportedReader = new Reader() { @Override public int read(char[] cbuf, int off, int len) throws IOException { return -1; // Dummy implementation } @Override public void close() throws IOException { // Dummy implementation } }; System.out.println(" unsupportedReader.markSupported() - " + unsupportedReader.markSupported() ); // Wrap the unsupported reader in a BufferedReader BufferedReader reader = new BufferedReader(unsupportedReader); // Check if mark and reset are supported if (reader.markSupported()) { System.out.println("Mark and reset are supported."); } else { System.out.println("Mark and reset are NOT supported."); } // Close the reader try { reader.close(); } catch (IOException e) { System.err.println("Error while closing the reader: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result −
unsupportedReader.markSupported() - false Mark and reset are supported.
Explanation
A custom Reader is created that does not support marking (markSupported() will return false).
The custom reader is wrapped in a BufferedReader, which overrides the behavior to support marking.
The output of markSupported() depends on the behavior of the underlying reader or the wrapper.
Key Points
BufferedReader always supports mark() and reset(), so markSupported() returns true for all instances of BufferedReader.
Wrapping an unsupported reader with a BufferedReader enables marking, but markSupported() can still provide information about the original reader's capabilities if directly accessed.
All examples demonstrate how markSupported() can be used to verify marking support and ensure safe usage of mark() and reset().