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

Syntax:

    public int read();
    public int read(byte[] b_arr, int offset, int length);
  • read() method is available in java.io 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.

Parameter(s):

  • 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.

Example:

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

import java.io.*;

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 
        buff_stm.read(b_arr, 0, 2);

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

        int val1 = 0;

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

            // Display ch
            System.out.println("buff_stm.read(): " + ch);
        }
        // Close the stream 
        fis.close();
        buff_stm.close();
    }
}

Output

buff_stm.read(b_arr,0,2): J
buff_stm.read(b_arr,0,2): a
buff_stm.read(): v
buff_stm.read(): a


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.