Advertisement



< Prev
Next >



Java - BufferedInputStream



File input/output operations consume a lot of important resources and are time consuming. Hence, reading a chunk of bytes out of a file and storing it in a local buffer for later processing is faster and less resource-intensive reading than reading one byte at a time from a file. Such buffering is used to speed up the I/O process and that's why the BufferedInputStream is used.

BufferedInputStream class is used to create such buffered input stream through which a chunk of bytes is read out of a file and stored in a local buffer for later processing, which is faster and less resource-intensive reading than by reading a chunk of bytes by using, let's say, FileInputStream.




Constructors :


BufferedInputStream(InputStream is)
This constructor creates a BufferedInputStream object to read the data out of a file, which is accessed by an InputStream, passed to this constructor as a parameter.

Example-:
File file= new File("D:/Textbook.txt");
FileInputStream fis= new FileInputStream(file);
BufferedInputStream fis= new BufferedInputStream(fis);
This example has created a BufferedInputStream object to read the data out of a file "D:/Textbook.txt" through InputStream i.e. FileInputStream, passed to constructor. As soon as this constructor is called, a large chunk of bytes is read out of file and stored in the local buffer of BufferedInputStream.




A point to remember:


FileInputStream is a subclass of InputStream




Methods for reading BufferedInputStream


Some methods that we generally use while working with BufferedInputStream are shown in the table below :

Methods Description
available()
This method gives the remaining number of bytes available to read from this input stream.

read()
This method reads one byte at a time from this input stream.

read(byte[])
This method reads a whole byte array at a time from this input stream.

close()
This method closes this input stream and also frees any resources connected with this input stream.




Advertisement





In the upcoming example, we are using BufferedInputStream to read an existing file called "TextBook.txt" through its path D:\\TextBook.txt. Let's say the contents of this file TextBook.txt are:
Hello there. How are you doing? 


//Java - Using BufferedInputStream to read(one byte at a time) out of an existing file.


import java.io.*;

class A
{
public static void main(String... ar)
{
	try
	{
		FileInputStream fis= new FileInputStream("D:\\Textbook.txt");
		BufferedInputStream bis= new BufferedInputStream(fis); //A local buffer is created which is big-
							//enough to hold chunk of bytes read out of file TextBook.txt

	//Determining the total number of bytes available to read
	System.out.println("Bytes available to read = "+ bis.available());

	int c;
	while( (c=bis.read())!=-1) //reading local buffer(one byte at a time) until EOF is reached
	System.out.print((char)c);

	//Closing the input streams
	bis.close();
	fis.close();
	}
	catch(IOException e)
	{
		System.out.println(e);
	}
}
}


Output is -:


Bytes available to read = 31
Hello there. How are you doing?


Program Analysis







In the next example, we are reading the contents of the same file TextBook.txt, but this time we are reading a whole byte array at once for faster reading by calling the read(byte[] b) method of BufferedInputStream.

//Java - Using BufferedInputStream to read a byte array out of a file.
 
import java.io.*;

class A
{
public static void main(String...ar)
{
	try
	{
		FileInputStream fin= new FileInputStream("D:\\TextBook.txt");
		BufferedInputStream bis= new BufferedInputStream(fis); //A local buffer is created which is big-
								//enough to hold chunk of bytes read out of file TextBook.txt

		//Determining the total number of bytes available to read
		int num= bis.available();
		
		System.out.println("Number of bytes available to read =" + num);
	
		byte b[]= new byte[num];
	
		int c;
	
		//read() method stops reading from file when EOF is reached i.e. -1	
		if((c=bis.read(b))!=-1) 
			System.out.println("Number of bytes read = " +c);

		String s= new String(b);
		System.out.println(s);
		fin.close();
	}
	catch(IOException e)
	{
		System.out.println(e);
	}
}
}


Output is -:


Number of bytes available to read =31
Number of bytes read = 31
Hello there. How are you doing?


Program Analysis






Please share this article -




< Prev
Next >
< ByteArrayOutputStream
BufferedOutputStream >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement