Java BufferedInputStream read() Method with Example

BufferedInputStream Class read() method: Here, we are going to learn about the read() method of BufferedInputStream Class with its syntax and example.
Submitted by Preeti Jain, on May 03, 2020

BufferedInputStream Class read() method


    public int read();
    public int read(byte[] b_arr, int offset, int length);
  • read() method is available in package.
  • read() method is responsible to read the next bytes of data from the BufferedInputStream.
  • read(byte[] b_arr, int offset, int length) method is used to read byte input stream till its length and paste it into the given byte array, starting at a given offset and it calls read() continuously of the underlying stream till any one of the condition become true.
  • These methods may throw an exception at the time of reading data to the stream.
    IOException: This exception may throw when getting any input/output error while reading from.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.


  • In the first case, read()
    • It does not accept any parameter.
  • In the second case, read(byte[] b_arr, int offset, int length)
    • byte [] b_arr – represents a byte array to be filled.
    • int offset – represents the offset (i.e. starting position to store).
    • int length – represents the at most number of bytes to be read.

Return value:

In both of the cases, the return type of the method is int,

  • In the first case, it returns the bytes read.
  • In the second case, it returns the total number of bytes to be read.


// Java program to demonstrate the example 
// of read() method of BufferedInputStream


public class ReadOfBIS {
    public static void main(String[] args) throws Exception {
        // To open text file by using 
        // FileInputStream
        FileInputStream fis = new FileInputStream("D:includehelp.txt");

        // Instantiates BufferedInputStream 
        BufferedInputStream buff_stm = new BufferedInputStream(fis);

        byte[] b_arr = new byte[2];

        // By using read(b_arr,offset,length) method is to
        // read bytes into an array from this BufferedInputStream, 0, 2);

        for (byte val: b_arr) {
            System.out.println(",0,2): " + (char) val);

        int val1 = 0;

        // By using read() method is to
        // read a byte from this BufferedInputStream
        while ((val1 = != -1) {
            // Convert int to char
            char ch = (char) val1;

            // Display ch
            System.out.println(" " + ch);
        // Close the stream 

Output,0,2): J,0,2): a v a


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.