Java Code Example for BufferedInputStream : markSupported() Method

Tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns true.

Below is the code example for BufferedInputStream markSupported() method –

/**********************************************************************************
 * 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);
			}
		}
	

	}

}

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.