Signup/Sign In

Java BufferedInputStream

In this tutorial, we will learn about BufferedInputStream class in Java. Java uses BufferedInputStream class to read data from the file, this class uses the buffer and so it is much faster and efficient. When we create an object of BufferedInputStream class, this creates an internal buffer array.

Syntax

The below code is the BufferedInputStream class declaration which extends FilterInputStream.

public class BufferedInputStream extends FilterInputStream  

Java BufferedInputStream Constructors

There are two overloading constructors for BufferedInputStream class is given in the table below.

Constructor Description
BufferedInputStream(InputStream in) It creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size) It creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Java BufferedInputStream Methods

The following table is showing the different methods supported by BufferedInputStream class and their uses.

Method Description
int available() This method returns an estimated number of bytes that can be read from the input stream without blocking by the next invocation method for the input stream.
int read() This method read the next byte of data from the input stream.
int read(byte[] b, int off, int ln) This method read the bytes from the specified byte-input stream into a specified byte array, starting with the given offset.
void close() This method closes the input stream and releases any of the system resources associated with the stream.
void reset() This method repositions the stream at a position the mark method was last called on this input stream.
void mark(int readlimit) This method sees the general contract of the mark method for the input stream.
long skip(long x) This method skips over and discards x bytes of data from the input stream.
boolean markSupported()
This method tests for the input stream to support the mark and reset methods.

Example of Java BufferedInputStream using read() Method

In the program given below, we are reading data from the file, to read data from the file we created an object of FileInputStream, and this object we passed to the constructor of BufferedInputStream class. Using read() method of BufferedInputStream class we are reading each character by character until the file ends. The end of the file will be equivalent to -1.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		try
		{    
			FileInputStream fin=new FileInputStream("E:\\studytonight\\myfile.txt");    
			BufferedInputStream in=new BufferedInputStream(fin);    
			int i;    
			while((i=in.read())!=-1)
			{    
				System.out.print((char)i);    
			}    
			in.close();    
			fin.close();    
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}    
	}  
}


Hello Studytonight.

In the above example, we learned how read() method works to read content from the files. Now we will learn about the skip() method.

Example of Java BufferedInputStream using skip() method

While reading data from a file we can skip initial bytes using skip() method, this method takes a parameter as a number of bytes to skip.

In the following program, we called method skip(5) so it will skip the first 5 bytes from the myfile.txt file.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		FileInputStream file = new FileInputStream("E:\\studytonight\\myfile.txt");
		BufferedInputStream buffer = new BufferedInputStream(file);
		// Skips for the 5 bytes
		buffer.skip(5);
		int i = buffer.read();
		while (i != -1) {
			System.out.print((char) i);
			i = buffer.read();
		}
		buffer.close();
	}  
}


FGHIJKL
Hello Studytonight
Hwllo World
1234567890

myfile.txt: This is the file that is used to read in the above example.

ABCDEFGHIJKL
Hello Studytonight
Hwllo World
1234567890

Example of Java BufferedInputStream using available() method

In the following example, we are using available() method to get available bytes to read from the file. Since the given file myfile.txt contains 20 bytes present to read it will give output as 20.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
        FileInputStream file = new FileInputStream("E:\\studytonight\\myfile.txt");        
        BufferedInputStream buffer = new BufferedInputStream(file);
        System.out.println("Total bytes available at beginning " + buffer.available());
        buffer.close();
	}  
}


Total bytes available at beginning 20

myfile.txt: This is the file that is used to read in the above example.

Hello Studytonight

Conclusion

In this tutorial, we learned how to read data from a file using BufferedInputStream class. When we create an object of BufferedInputStream class, this creates an internal buffer array. Since we are using a buffer in this class it is much faster than other ways.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.