Java - InputStream read(byte[] b) method



Description

The Java InputStream read(byte[] b) method reads multiple bytes into a byte array. This is more efficient than reading one byte at a time. Reads up to b.length bytes into the byte array. Returns the number of bytes actually read. Returns -1 when EOF (End of File) is reached.

Declaration

Following is the declaration for java.io.InputStream.read(byte[] b) method −

public int read(byte[] b)

Parameters

b − The destination byte array.

Return Value

The method returns the number of bytes actually read into the buffer, or -1 if the end of the stream is reached.

Exception

  • IOException − If an I/O error occurs.

  • NullPointerException − If b is null.

Example - Usage of InputStream read(byte[] b) method

The following example shows the usage of Java InputStream read(byte[] b) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      byte[] buffer = new byte[5];
      char c;
      
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         System.out.println("Characters printed:");
         
         // read stream data into buffer
         is.read(buffer);
         
         // for each byte in the buffer
         for(byte b:buffer) {
         
            // convert byte to character
            c = (char)b;
            
            // prints character
            System.out.print(c);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Characters printed:
ABCDE

Example - Reading Multiple Bytes Using FileInputStream

The following example shows the usage of Java InputStream read(byte[] b) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new FileInputStream("example.txt")) {
         byte[] buffer = new byte[10]; // Buffer to hold 10 bytes
         int bytesRead = inputStream.read(buffer); // Read into buffer

         if (bytesRead != -1) {
            System.out.println("Bytes read: " + bytesRead);
            System.out.println("Data: " + new String(buffer, 0, bytesRead)); // Convert to string
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "HelloMicroservices")

Let us compile and run the above program, this will produce the following result−

Bytes read: 10
Data: HelloMicro

Explanation

  • Uses FileInputStream, a subclass of InputStream.

  • Reads up to 10 bytes into a byte array.

  • Converts the read bytes into a string and prints them.

Example - Reading a File in Chunks Using BufferedInputStream

The following example shows the usage of Java InputStream read(byte[] b) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         byte[] buffer = new byte[5]; // Buffer for 5 bytes

         int bytesRead;
         while ((bytesRead = inputStream.read(buffer)) != -1) { // Read in chunks
            System.out.print(new String(buffer, 0, bytesRead)); // Print read data
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Microservices")

Let us compile and run the above program, this will produce the following result−

Microservices

Explanation

  • Uses BufferedInputStream, a FilterInputStream subclass.

  • Reads the file in chunks of 5 bytes at a time.

  • Continues reading until EOF (-1) is reached.

java_io_inputstream.htm