Java.io.BufferedInputStream.read() Method


Advertisements


Description

The java.io.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.

Example

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

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("c:/test.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();
      }
   }
}

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

ABCDE  

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

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

java_io_bufferedinputstream.htm

Advertisements