Last Updated : 23 Jul, 2025
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. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.
Constructor and DescriptionSyntax:public int available() throws IOException Returns: an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking. Throws: IOException
Syntax:public void close() throws IOException Overrides: close in class FilterInputStream Throws: IOException
Syntax:public void mark(int readlimit) Overrides: mark in class FilterInputStream Parameters: readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
Syntax:public boolean markSupported() Overrides: markSupported in class FilterInputStream Returns: a boolean indicating if this stream type supports the mark and reset methods.
Syntax:public int read() throws IOException Returns: the next byte of data, or -1 if the end of the stream is reached. Throws: IOException
Syntax:public int read(byte[] b, int off, int len) throws IOException Parameters: b - destination buffer. off - offset at which to start storing bytes. len - maximum number of bytes to read. Returns: the number of bytes read, or -1 if the end of the stream has been reached. Throws: IOException
Syntax:public void reset() throws IOException Overrides: reset in class FilterInputStream Throws: IOException
Syntax:public long skip(long n) throws IOException Parameters: n - the number of bytes to be skipped. Returns: the actual number of bytes skipped. Throws: IOException
// Java program to demonstrate working of BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
// Java program to demonstrate BufferedInputStream methods
class BufferedInputStreamDemo
{
public static void main(String args[]) throws IOException
{
// attach the file to FileInputStream
FileInputStream fin = new FileInputStream("file1.txt");
BufferedInputStream bin = new BufferedInputStream(fin);
// illustrating available method
System.out.println("Number of remaining bytes:" +
bin.available());
// illustrating markSupported() and mark() method
boolean b=bin.markSupported();
if (b)
bin.mark(bin.available());
// illustrating skip method
/*Original File content:
* This is my first line
* This is my second line*/
bin.skip(4);
System.out.println("FileContents :");
// read characters from FileInputStream and
// write them
int ch;
while ((ch=bin.read()) != -1)
System.out.print((char)ch);
// illustrating reset() method
bin.reset();
while ((ch=bin.read()) != -1)
System.out.print((char)ch);
// close the file
fin.close();
}
}
Output:
Number of remaining bytes:47 FileContents : is my first line This is my second line This is my first line This is my second lineNext Article: Java.io.BufferedOutputStream class in Java
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4