BufferedInputStream read() method in Java with Examples Last Updated : 05 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // Java program to illustrate // BufferedInputStream read() method import java.io.*; public class GFG { public static void main(String[] args) throws Exception { // Create input stream 'demo.txt' // for reading containing // text "GEEKSFORGEEKS" FileInputStream inputStream = new FileInputStream("c:/demo.txt"); // Convert inputStream to // bufferedInputStream BufferedInputStream buffInputStr = new BufferedInputStream( inputStream); // Read until a single byte is available while (buffInputStr.available() > 0) { // Read the byte and // convert the integer to character char c = (char)buffInputStr.read(); // Print the characters System.out.println("Char : " + c); } } } Input: Output: 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 // Java program to illustrate // BufferedInputStream // read(byte int int) method import java.io.*; public class GFG { public static void main(String[] args) { // Create input stream 'demo.txt' // for reading containing // text "GEEKSFORGEEKS" FileInputStream inputStream = new FileInputStream("c:/demo.txt"); // Convert inputStream to // bufferedInputStream BufferedInputStream buffInputStr = new BufferedInputStream( inputStream); // Read number of bytes available int rem_byte = buffInputStr.available(); // Byte array is declared byte[] barr = new byte[rem_byte]; // Read byte into barr, // starts at offset 1, // 5 bytes to read buffInputStr.read(barr, 1, 5); // For each byte in barr for (byte b : barr) { if (b == (byte)0) b = (byte)'-'; System.out.print((char)b); } } } Input: Output: 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) Comment More infoAdvertise with us Next Article DataInputStream readInt() method in Java with Examples P pp_pankaj Follow Improve Article Tags : Java Java-Functions Java-IO package Practice Tags : Java Similar Reads BufferedInputStream reset() method in Java with Examples The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class. General Contract: There are two cases: If mark() and reset() are suppor 3 min read DataInputStream read() method in Java with Examples The read() method of DataInputStream class in Java is of two types: read(byte[] b) method of DataInputStream class in Java is used to read bytes from the input stream and store them into the buffer byte array.This read() method returns the number of bytes actually read as an integer type. This metho 4 min read ByteArrayInputStream read() method in Java with Examples The read() method of ByteArrayInputStream class in Java is used in two ways: 1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read into the form of an integer and if the input stream is e 3 min read BufferedInputStream close() method in Java with Examples The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch 2 min read DataInputStream readInt() method in Java with Examples The readInt() method of DataInputStream class in Java is used to read four input bytes and returns a integer value. This method reads the next four bytes from the input stream and interprets it into integer type and returns. Syntax: public final int readInt() throws IOException Specified By: This me 2 min read BufferedInputStream skip(long) method in Java with Examples The skip(long) method of BufferedInputStream class in Java is used to skip n bytes of data from the buffered input stream. The number of bytes skipped is stored and returned as long type. The termination condition involves either of the two: Either reading into a byte array until n-bytes are covered 2 min read Like