Program to How to use Mark and Reset methods with BufferedInputStream

The mark() method mark a position in the input to which the stream can be reset by calling the reset() method
OR
The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

Eg - Let's say we are reading 'abcd'
read 'a', mark() 'b', read 'b', read 'c',
call reset()
now we will start reading from 'b'



In short : mark() and reset() allows us to go backward i.e. to the stream that has already been read.


We will read c:/myFile.txt and c:/myFile.txt looks like this >
abcd



Program to use Mark and Reset methods of BufferedInputStream >

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class BufferedInputStreamMarkAndResetMethods {
   public static void main(String[] args) {
      FileInputStream fis = null;
          BufferedInputStream bis = null;
         
          try {
                 fis = new FileInputStream("c:/myFile.txt");
                 bis = new BufferedInputStream(fis);
                 //check whether InputStream supports mark/reset or not
                 if(!bis.markSupported()){
                       System.out.println("mark/reset not supported");
                       System.exit(0);
                 }
                
                 System.out.println( ((char) ((char)bis.read()) ) ); // Print -> a
                 // mark -> b
                 bis.mark(1);
                 System.out.println( ((char) ((char)bis.read()) ) ); // Print -> b
                 System.out.println( ((char) ((char)bis.read()) ) ); // Print -> c
                 System.out.println("call reset() first time");
                 // reset to marked point i.e. to b
                 bis.reset();
                 System.out.println( ((char)bis.read()) ); // Print -> b
                 // mark -> Line 3
                 bis.mark(0);
                 System.out.println( ((char)bis.read()) ); // Print -> c
                 System.out.println( ((char)bis.read()) ); // Print -> d
                 System.out.println("call reset() second time");
                 // reset to marked point i.e. to Line 3
                 bis.reset();
                 System.out.println( ((char)bis.read()) ); // Print -> c
                 bis.close();
                
          } catch (IOException e) {
        e.printStackTrace();
       }
   }
}
/*OUTPUT
a
b
c
call reset() first time
b
c
d
call reset() second time
c
d
*/


Note : before making call to reset() method, stream must have been marked using mark() method.   


RELATED LINKS>

Program to How to use Mark and Reset methods with BufferedReader

Difference between FileInputStream and BufferedInputStream in java file IO

Program to Read text from file using BufferedReader's read() and readLine() methods in java file IO


No comments:

Post a Comment