Java - Reader markSupported() method



Description

The Java Reader markSupported() method tells whether this stream supports the mark() operation.

Declaration

Following is the declaration for java.io.Reader.markSupported() method.

public boolean markSupported()

Parameters

NA

Return Value

This method returns true if and only if this stream supports the mark operation.

Exception

  • IOException − If the stream does not support mark(), or if some other I/O error occurs.

Example - Usage of Reader markSupported() method

The following example shows the usage of Reader markSupported() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class ReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // change line
         System.out.println();

         // check if mark is supported
         System.out.println( reader.markSupported());

         // close the stream
         reader.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello
true

Example - Using BufferedReader (Supports Mark)

The following example shows the usage of Reader markSupported() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;

public class ReaderDemo {
   public static void main(String[] args) {
      String data = "Hello World";
      BufferedReader reader = new BufferedReader(new StringReader(data));

      System.out.println("markSupported(): " + reader.markSupported());

      try {
         reader.mark(5); // This is safe
         System.out.println("Marked at start");

         System.out.print((char) reader.read()); // H
         System.out.print((char) reader.read()); // e

         reader.reset(); // Resets to 'H'
         System.out.print("\nAfter reset: ");
         System.out.print((char) reader.read()); // H again
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

markSupported(): true
Marked at start
He
After reset: H

Explanation

  • BufferedReader supports mark() and reset(), so markSupported() returns true.

  • We use mark() safely and reset to re-read characters.

Example - Using A Custom Reader (Which Does NOT Support Mark)

The following example shows the usage of Reader markSupported() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.Reader;
import java.io.IOException;

public class ReaderDemo {
   public static void main(String[] args) {
      // Create an anonymous Reader that does not support mark/reset
      Reader reader = new Reader() {
         private String data = "This is a test";
         private int pos = 0;

         @Override
         public int read(char[] cbuf, int off, int len) throws IOException {
            if (pos >= data.length()) return -1;
               int charsToRead = Math.min(len, data.length() - pos);
               data.getChars(pos, pos + charsToRead, cbuf, off);
               pos += charsToRead;
               return charsToRead;
            }

         @Override
         public void close() throws IOException {
            // Nothing to close in this in-memory example
         }

         @Override
         public boolean markSupported() {
            return false;
         }
      };

      System.out.println("markSupported(): " + reader.markSupported());

      try {
         if (reader.markSupported()) {
            reader.mark(10); // will not happen
         } else {
            System.out.println("Mark not supported, skipping mark/reset.");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

markSupported(): false
Mark not supported, skipping mark/reset.

Explanation

  • FileReader does not support marking.

  • We check markSupported() to avoid calling mark() and reset() and throwing an exception.

java_io_reader.htm
Advertisements