Thursday, September 4, 2025
HomeLanguagesJavaDataInputStream readInt() method in Java with Examples

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 method is specified by readInt() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the int value interpreted by the next four bytes of the input stream.

Exceptions:

  • EOFException – It throws EOFException if the input stream is ended before four bytes can be read.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readInt() method in DataInputStream class in IO package:

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




// Java program to illustrate
// DataInputStream readInt() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create integer array
        int[] buf = { 10, 20, 30, 40, 50 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (int b : buf) {
            // Write integer value to
            // the dataOutputStream
            dataOutputStr.writeInt(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print integer values
            System.out.println(
                dataInputStr.readInt());
        }
    }
}


Output:

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




// Java program to illustrate
// DataInputStream readInt() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create integer array
        int[] buf = { 191, 225, 480, 763, 500 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (int b : buf) {
            // Write integer value to
            // the dataOutputStream
            dataOutputStr.writeInt(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print integer values
            System.out.println(
                dataInputStr.readInt());
        }
    }
}


Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readInt()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS