Signup/Sign In
PUBLISHED ON: MARCH 25, 2021

Java BufferedInputStream available() Method

In this tutorial, we will learn about the available() method of BufferedInputStream class in Java. This method returns the number of bytes that remained to read from an input stream without blocking by the next invocation of a method for this input stream.

Syntax

This is the syntax declaration of the available() method it returns the number of bytes that remained to read from this input stream without blocking

public int available()

Example: Java BufferedInputStream 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

Example: Java BufferedInputStream available Method

In the following code, we are running the loop while the number of available bytes to read is more than 0, in each iteration, we read the character and also printing the number of available bytes to read. The important observation to note is when in each iteration the available bytes are decreasing.

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\\output.txt");        
		BufferedInputStream buffer = new BufferedInputStream(file);
		while(buffer.available()>0)
		{
			System.out.println("Available bytes = " + buffer.available() );
			char ch =  (char)buffer.read();
			System.out.println("The character read = " + ch );
		}
		buffer.close();
	}  
}


Available bytes = 18
The character read = H
Available bytes = 17
The character read = e
Available bytes = 16
The character read = l
Available bytes = 15
The character read = l
Available bytes = 14
The character read = o
Available bytes = 13
The character read =
Available bytes = 12
The character read = S
Available bytes = 11
The character read = t
Available bytes = 10
The character read = u
Available bytes = 9
The character read = d
Available bytes = 8
The character read = y
Available bytes = 7
The character read = t
Available bytes = 6
The character read = o
Available bytes = 5
The character read = n
Available bytes = 4
The character read = i
Available bytes = 3
The character read = g
Available bytes = 2
The character read = h
Available bytes = 1
The character read = t

Conclusion

In this tutorial we learned the available method of BufferedInputStream class in Java, this method returns the number of bytes that remained to read from an input stream without blocking by the next invocation of a method for this input stream.



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.