Java - BufferedInputStream read() method



Description

The Java BufferedInputStream read() method reads the next byte of data from the input stream.

Declaration

Following is the declaration for java.io.BufferedInputStream.read() method.

public int read()

Parameters

NA

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDE

Example - Using read() method to read from File

The following example shows the usage of Java BufferedInputStream read() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream inStream = null;
      BufferedInputStream bis = null;
      
      try {
         // open input stream test.txt for reading purpose.
         inStream = new FileInputStream("example.txt");

         // input stream is converted to buffered input stream
         bis = new BufferedInputStream(inStream);			

         // read until a single byte is available
         while(bis.available()>0) {
         
            // read the byte and convert the integer to character
            char c = (char)bis.read();

            // print the characters
            System.out.println("Char: "+c);;
         }
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {		
         // releases any system resources associated with the stream
         if(inStream!=null)
            inStream.close();
         if(bis!=null)
            bis.close();
      }
   }
}

Output

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

Char: A
Char: B
Char: C
Char: D
Char: E

Example - Using read() method to Read One Byte at a Time

The following example shows the usage of Java BufferedInputStream read() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

public class BufferedInputStreamDemo {
    public static void main(String[] args) {
        byte[] data = "Hello, BufferedInputStream!".getBytes();

         // open input stream test.txt for reading purpose.
        try (BufferedInputStream bis = new BufferedInputStream(
		   new ByteArrayInputStream(data))) {
            System.out.println("Reading data one byte at a time:");

            int byteData;
            while ((byteData = bis.read()) != -1) { // Read one byte at a time
                System.out.print((char) byteData); // Convert byte to char for printing
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

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

Reading data one byte at a time:
Hello, BufferedInputStream!

Explanation

  • Stream Source− A BufferedInputStream wraps a ByteArrayInputStream that contains the byte array data.

  • Behavior

    • The read() method reads one byte at a time and returns its integer value.

    • When the end of the stream is reached, read() returns -1.

  • Output: The characters are printed to the console one at a time.

java_io_bufferedinputstream.htm
Advertisements