Below code shows how to use BufferedInputStream: reset() method in java –
/**********************************************************************************
* Created on Nov, 2004 Copyright(c) https://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 https://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);
}
}
}
}