Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaBufferedInputStream read() method in Java with Examples

BufferedInputStream read() method in Java with Examples

  1. 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() 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:
  2. 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) 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)

RELATED ARTICLES

Most Popular

Recent Comments