BufferedInputStream read() method in Java with Examples

- read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time.
Syntax:
public int read()
Overrides:
It overrides read() method of FilterInputStream class.Parameters: This method does not accept any parameter.
Return value: This method does not return any value.
Exception: This method throws IOException if the input stream has been closed by invoking its close() method or an I/O error occurs.
Below program illustrates read() method in BufferedInputStream class in IO package:
Program: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// BufferedInputStream read() methodimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args)throwsException{// Create input stream 'demo.txt'// for reading containing// text "GEEKSFORGEEKS"FileInputStream inputStream=newFileInputStream("c:/demo.txt");// Convert inputStream to// bufferedInputStreamBufferedInputStream buffInputStr=newBufferedInputStream(inputStream);// Read until a single byte is availablewhile(buffInputStr.available() >0) {// Read the byte and// convert the integer to charactercharc = (char)buffInputStr.read();// Print the charactersSystem.out.println("Char : "+ c);}}} - read(byte[ ] b, int off, int len) method of BufferedInputStream class in Java is used to read bytes from the byte-input stream into the specified byte array which starts at the offset given by user. It is basically used to start reading after preserving the characters in an array.
Implementation:
- In the implementation of this method, read() method is called again and again. While calling this method if an IOException is found then it returns the exception from the call to the read(byte[ ] b, int off, int len) method.
- If further any IOException is found then it catches the exception and input file is supposed to be ended.
- The bytes that are read up to that point are stored into byte array b and the number of bytes read before the occurrence of exception is returned.
Syntax:
public int read(byte[] b, int off, int len)Overrides:
It overrides read() method of FilterInputStream class.Parameters: This method accepts three parameters.
- b – It represents destination buffer.
- off – It represents offset at which storing bytes would be started.
- len – It represents the maximum number of bytes to read.
Return value: This method does not return any value.
Exception: This method throws IOException if the input stream has been closed by invoking its close() method or an I/O error occurs.
Below program illustrates read(byte, int, int) method in BufferedInputStream class in IO package:
Program: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// BufferedInputStream// read(byte int int) methodimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args){// Create input stream 'demo.txt'// for reading containing// text "GEEKSFORGEEKS"FileInputStream inputStream=newFileInputStream("c:/demo.txt");// Convert inputStream to// bufferedInputStreamBufferedInputStream buffInputStr=newBufferedInputStream(inputStream);// Read number of bytes availableintrem_byte = buffInputStr.available();// Byte array is declaredbyte[] barr =newbyte[rem_byte];// Read byte into barr,// starts at offset 1,// 5 bytes to readbuffInputStr.read(barr,1,5);// For each byte in barrfor(byteb : barr) {if(b == (byte)0)b = (byte)'-';System.out.print((char)b);}}}
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#read()
2. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#read(byte%5B%5D, int, int)




