Friday, December 27, 2024
Google search engine
HomeLanguagesJavaDataInputStream read() method in Java with Examples

DataInputStream read() method in Java with Examples

The read() method of DataInputStream class in Java is of two types:

  1. 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 method returns -1 if the input stream is ended and no more data is available to read. This method throws an exception if the byte array is null.

    Syntax:

    public final int read(byte[] b)
                   throws IOException
    

    Overrides: This method overrides the read() method of FilterInputStream class.

    Parameters: This method accepts one parameter b which represents the byte array into which the data is to read.

    Return value: This method returns the number of bytes actually read. It returns -1 if the input stream is ended and no more data is available to read.

    Exceptions:

    • NullPointerException – It throws NullPointerException if byte array is null.
    • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

    Below program illustrates read(byte[]) method in DataInputStream class in IO package:

    Program: Assume the existence of file “c:/demo.txt”.




    // Java program to illustrate
    // DataInputStream read(byte[]) method
    import java.io.*;
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create input stream 'demo.txt'
            // for reading containing
            // text "GEEKSFORGEEKS"
            FileInputStream inputStream
                = new FileInputStream(
                    "c:/demo.txt");
      
            // Convert inputStream to
            // DataInputStream
            DataInputStream dataInputStr
                = new DataInputStream(
                    inputStream);
      
            // Count the total bytes
            // form the input stream
            int count = inputStream.available();
      
            // Create byte array
            byte[] b = new byte[count];
      
            // Read data into byte array
            int bytes = dataInputStr.read(b);
      
            // Print number of bytes
            // actually read
            System.out.println(bytes);
      
            for (byte by : b) {
                // Print the character
                System.out.print((char)by);
            }
        }
    }

    
    
    Input:
    Output:
  2. read(byte[] b, int offset, int length) method of DataInputStream class in Java is used to read specified number of 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 method returns -1 if the input stream is ended and no more data is available to read. This method throws an exception if the byte array is null.

    Syntax:

    public final int read(byte[] b,
                          int offset,
                          int length)
                   throws IOException
    

    Overrides: This method overrides the read() method of FilterInputStream class.

    Parameters: This method accepts three parameters:

    • b – It represents the byte array into which the data is to read.
    • offset – It represents the starting index in the byte array.
    • length – It represents the total number of bytes to be read.

    Return value: This method returns the number of bytes actually read. It returns -1 if the input stream is ended and no more data is available to read.

    Exceptions:

    • NullPointerException – It throws NullPointerException if byte array is null.
    • IndexOutOfBoundsException – It throws IndexOutOfBoundsException if If offset is negative or length is negative or length is greater than the difference of length of byte array and offset.
    • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

    Below program illustrates read(byte[], int, int) method in DataInputStream class in IO package:

    Program: Assume the existence of file “c:/demo.txt”.




    // Java program to illustrate
    // DataInputStream read(byte[], int, int) method
    import java.io.*;
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create input stream 'demo.txt'
            // for reading containing
            // text "GEEKSFORGEEKS"
            FileInputStream inputStream
                = new FileInputStream(
                    "c:/demo.txt");
      
            // Convert inputStream to
            // DataInputStream
            DataInputStream dataInputStr
                = new DataInputStream(
                    inputStream);
      
            // Count the total bytes
            // form the input stream
            int count = inputStream.available();
      
            // Create byte array
            byte[] b = new byte[count];
      
            // Read data into byte array
            int bytes = dataInputStr.read(b, 4, 5);
      
            // Print number of bytes
            // actually read
            System.out.println(bytes);
      
            for (byte by : b) {
                // Print the character
                System.out.print((char)by);
            }
        }
    }

    
    
    Input:
    Output:

References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#read(byte%5B%5D)
2. https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#read(byte%5B%5D, int, int)

RELATED ARTICLES

Most Popular

Recent Comments