How to use BufferedInputStream: reset() method in java code?

Below code shows how to use BufferedInputStream: reset() method in java –

/**********************************************************************************
 * Created on Nov, 2004 Copyright(c) http://kodehelp.com All Rights Reserved.
 **********************************************************************************/
package com.kodehelp.java.io;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
 * @author http://kodehelp.com
 *
 */
public class BufferedInputStreamReset {
 
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        BufferedInputStream bufferedInputStream = null;
        try{
            bufferedInputStream = new BufferedInputStream(new FileInputStream("/test.txt"));
            System.out.println("First Character in Buffer ::"+(char)bufferedInputStream.read());
            bufferedInputStream.mark(0);
            System.out.println("Next Character in buffer is  : "+(char)bufferedInputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)bufferedInputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)bufferedInputStream.read());
 
            System.out.println("reset method called");
 
            bufferedInputStream.reset();
 
            System.out.println("Next Character in buffer is  : "+(char)bufferedInputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)bufferedInputStream.read());
 
        }catch(FileNotFoundException e){
            System.out.println("File not found" + e);
        }catch(IOException ioe){
            System.out.println("Exception while reading the file " + ioe);
        }finally{
            //close the BufferedInputStream using close method
            try{
                if(bufferedInputStream != null)
                    bufferedInputStream.close();
 
            }catch(IOException ioe){
                System.out.println("Error while closing the stream : " + ioe);
            }
        }
    }
 
}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.