Below code shows how to use BufferedInputStream: markSupported method in java –
[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 BufferedInputStreamMarkSupported {
/**
* @param args
*/
public static void main(String[] args) {
BufferedInputStream inputStream = null;
try{
inputStream = new BufferedInputStream(new FileInputStream("/test.txt"));
System.out.println("First Character in Buffer ::"+(char)inputStream.read());
if(inputStream.markSupported())
inputStream.mark(0);
System.out.println("Next Character in buffer is : "+(char)inputStream.read());
System.out.println("Next Character in buffer is : "+(char)inputStream.read());
System.out.println("Next Character in buffer is : "+(char)inputStream.read());
System.out.println("reset method called");
if(inputStream.markSupported())
inputStream.reset();
System.out.println("Next Character in buffer is : "+(char)inputStream.read());
System.out.println("Next Character in buffer is : "+(char)inputStream.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(inputStream != null)
inputStream.close();
}catch(IOException ioe){
System.out.println("Error while closing the stream : " + ioe);
}
}
}
}
[/java]