markSupported() mark() reset() Java


Rereading from the Same Position

The same contents of a file can be again and again read by marking the position with the methods of StringBufferInputStream, inherited from its super class InputStream.

A file can be reread (reading again) from a particular location. Here, the file position is marked; execution control is taken back to the same position and reread.

markSupported() mark() reset() Java

Using these methods, helpful to read the data, the file position is marked, control is shifted back to the position and read from that position.

In the following application, the file Security.txt file is re-read again using MarkAndReset.java.

File Name: Security.txt

Security is a shared effort. Every one’s $ participation is utmost important.

File Name: MarkAndReset.java

import java.io.*;
public class MarkAndReset
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fistream = new FileInputStream("Security.txt");
    BufferedInputStream bistream = new BufferedInputStream(fistream);

    System.out.print("All the contents\n\t");     
    int temp, counter = 0;
    while(( temp = bistream.read() ) != -1)
    {
      counter++;
      if(temp == '$')
      {
        if(bistream.markSupported())
        {
          bistream.mark(counter);
        }
       }
       System.out.print((char) temp);
     }
     bistream.reset();
     System.out.print("\nReading again after position $ is marked\n\t");
     while(( temp = bistream.read()) != -1)
     {
       System.out.print((char) temp);
     }       

     System.out.print("\nAgain and again read\n\t");
     bistream.reset();
     while(( temp = bistream.read() ) != -1)
     {
       System.out.print((char) temp);
     }
     bistream.close();
     fistream.close();
   }
}    


markSupported() mark() reset() Java
Output screen onmarkSupported() mark() reset() Java

       FileInputStream fistream = new FileInputStream("Security.txt");
       BufferedInputStream bistream = new BufferedInputStream(fistream);

The file, Security.txt contains some data of which a few important words are re-read. The FileInputStream is chained to BufferedInputStream as this stream is the best suitable by performance-wise. Reading with BufferedInputStream is few thousand times speedier than reading with FileInputStream.

             
       if(temp == '$')
       {
         if(bistream.markSupported())
         {
           bistream.mark(counter);
         }
       }

'$' symbol is chosen in the file from where the contents are read again. Every byte read by read() method is compared with '$' symbol and when '$' is encountered, the position is marked with the mark() method of BufferedInputStream. For code robustness and systematic way of code development, the BufferedInputStream is checked whether it supports the marking using markSupported() method which returns true if the stream supports.

       bistream.reset();

The reset() method places the file pointer exactly at the same position where we marked with mark() method. From here, the file is read again with the usual while loop. In the program, the contents from '$' symbol are read again and again (2 times) to have grip over the marking and reading.

2 thoughts on “markSupported() mark() reset() Java”

  1. This is because all the stream implementations do not support marking and in cases where it does not support it, it will
    throw an IOException with the error message “mark() not supported”. To avoid that, we check that using an if condition

  2. sir,

    without markSupported also we can write above program no ?? then what is the need of markSupported()…

Leave a Comment

Your email address will not be published.