java.io.BufferedInputStream Class

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. The internal buffer is refilled as necessary from the contained input stream, many bytes at a time. 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.

Buffering I/O is a very common performance optimization. Java’s BufferedInputStream class allows you to "wrap" any InputStream into a buffered stream to improve performance.

java.io.BufferedInputStream class Example
/* Following example shows how to read the text from file using java BufferedInputStream. */
/* Save with file name BufferedInputStreamExample.java */

import java.io.*;

public class BufferedInputStreamExample
{
	public static void main(String args[])
	{
		//BufferedInputStream DECLARATION
		BufferedInputStream bis = null;

		//FileInputStream DECLARATION
		FileInputStream fis = null;

		try
		{
			int ch=0;

			fis = new FileInputStream("BufferedInputStreamExample.java");

			bis = new BufferedInputStream(fis);

			while((ch = bis.read()) != -1)
			{
				//CONVERT byte TO char
				System.out.print( (char) ch);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}
                        

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

java.io.BufferedInputStream class Example 2
/* Following example shows how to read total bytes from file into array using java BufferedInputStream. */
/* Save with file name BufferedInputStreamExample2.java */

import java.io.*;

public class BufferedInputStreamExample2
{
	public static void main(String args[])
	{
		//java.io.BufferedInputStream DECLARATION
		java.io.BufferedInputStream bis = null;

		//FileInputStream DECLARATION
		FileInputStream fis = null;

		//ARRAY TO READ BYTES
		byte bytebuff[] = null;

		try
		{
			fis = new FileInputStream("BufferedInputStreamExample2.java");

			bis = new BufferedInputStream(fis);

			//GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
			bytebuff = new byte[fis.available()];

			//READ TOTAL BYTES FROM FILE
			bis.read(bytebuff, 0,fis.available());

			//PRINT CHARACTERS FROM ARRAY
			for(int j=0;j<bytebuff.length;j++)
			{
				//CONVERT byte TO char
				System.out.print( (char) bytebuff[j]);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				//RELEASE ARRAY MEMORY
				bytebuff = null;

				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}
                        
java.io.BufferedInputStream class Example 3
/* Following example shows how to read total bytes from file into array using java BufferedInputStream. */
/* Save with file name BufferedInputStreamExample3.java */

import java.io.*;

public class BufferedInputStreamExample3
{
	public static void main(String args[])
	{
		String str = "This is a &copy; copyright symbol but not &copy not.\n";

		//java.io.BufferedInputStream DECLARATION
		java.io.BufferedInputStream bis = null;

		//java.io.ByteArrayInputStream DECLARATION
		ByteArrayInputStream bais = null;

		//ARRAY TO READ BYTES
		byte bytebuff[] = null;

		try
		{

			bytebuff = str.getBytes();

			bais = new ByteArrayInputStream(bytebuff);

			bis = new BufferedInputStream(bais);

			while(
			{
				//CONVERT byte TO char
				System.out.print( (char) bytebuff[j]);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			//SHOULD CLOSE STREAMS FINALLY
			try
			{
				//RELEASE ARRAY MEMORY
				bytebuff = null;

				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}