DataInputStream in Java

The DataInputStream class in Java used to read data in primitive type size bytes from the underlying InputStreams. It has special methods to read primitive type size bytes.

It is a subclass of FilterInputStream and implements the DataInput interface. FilterInputStream is the subclass of InputStream. These all classes are available from Java 1.0 version onwards. InputStream class is used to read data in binary form. DataInput interface having several methods to read bytes from the binary input stream and convert them into the corresponding Java primitive type. Other implemented interfaces are:- Closeable, AutoCloseable

public class DataInputStream
extends FilterInputStream
implements DataInput

Constructors of DataInputStream

The FileInputStream is the basic source for all input stream classes which can directly make a connection between Java application and file. Similar to the other input stream classes, the DataInputStream class also depends on the FileInputStream class to make a connection between the Java application and file. DataInputStream class has only one constructor which needs InputStream as a parameter.

public DataInputStream(InputStream in) {
   super(in);
}

Methods of DataInputStream class

The readXxx() methods are given to read the different primitive type values. Actually, the readXxx() method read the number of bytes from a file based on the data type. For example, if we call the readInt() method then it reads 4 bytes available in the file. If 4 bytes are not available then it throws EOFException.

Methods to read as different primitive types values,

Return typeMethod
bytereadByte()
intreadUnSignedByte()
shortreadShort()
intreadUnSignedShort()
intreadInt()
longreadLong()
floatreadFloat()
doublereadDouble()
charreadChar()
booleanreadBoolean()
StringreadUTF()

To read String value readLine() is also there but it is deprecated because it doesn’t properly convert bytes to characters.

Above all methods throws IOException if the stream has been closed or another I/O error occurs. It also throws EOFException if the input stream reaches the end before reading the required bytes. The readUTF() also throws UTFDataFormatException if the bytes do not represent a valid modified UTF-8 encoding of a string.

Other methods of Java DataInputStream are,

Modifier and TypeMethodDescription
intread(byte[] b)Read some number of bytes store them into buffer array b.
intread(byte[] b, int off, int len)Read up to len bytes of data and store them into buffer array b.
voidreadFully(byte[] b)Similar to read(byte[] b)
voidreadFully(byte[] b, int off, int len)Similar to read(byte[] b, int off, int len)
static StringreadUTF(DataInput in)Reads Unicode character string encoded in modified UTF-8
format and returns them as String.
intskipBytes(int n)Skip n bytes of data.

The read(byte[] b) and read(byte[] b, int off, int len) method read some/len number of bytes data, store then into buffer array b, and then returns the number of bytes available in buffer array b. The readFully(byte[] b) and readFully(byte[] b, int off, int len) do the same but it doesn’t return anything.

These methods throw IOException if the stream has been closed or another I/O error occurs. The readUTF(DataInput in) also throws UTFDataFormatException if the bytes do not represent a valid modified UTF-8 encoding of a string.

Java program example using DataInputStream

Important points

1) To read data as primitive types using readXxx() methods, first of all, data must be written to the file as primitive types using writeXxx() methods of DataOutputStream class.

2) readXxx() methods must be called in the same order in which the writeXxx() methods are called otherwise wrong value will be returned or EOFException is raised.

In this program, we are using file.txt which is written by the writeXxx() method of DataOutputStream class. We will call readXxx() in the same order as we used writeXxx(). Note:- Whenever are typing characters into the file, and it won’t be treated as a byte. They are characters, not the bytes. See the program given in DataOutputStream class to generate the output.txt. The content in the output.txt is,

0000 0061 0000 0000 0000 0064 42c7 cccd
4057 e000 0000 0000 0061 0100 0f6b 6e6f
7770 726f 6772 616d 2e63 6f6d 4865 6c6c
6f2c 2077 6f72 6c64 2100 4a00 6100 7600
6100 2000 7000 7200 6f00 6700 7200 6100
6d61

Below application demonstrates reading data in primitive type size bytes using DataInputStream class.

// DataInputStream class demonstration
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class DISTest {
   public static void main(String[] args) 
           throws IOException, FileNotFoundException {

      // create FileInputStream object
      // constructor for overidden mode
      FileInputStream fis = 
           new FileInputStream("output.txt");

      // constructor to connect file in append mode
      /* FileInputStream fis = 
           new FileInputStream("output.txt", true); */

      // create DataInputStream object
      DataInputStream dis =  new DataInputStream(fis);

      // read int data
      int n1 = dis.readInt();
      System.out.println(n1);

      // read long
      System.out.println(dis.readLong());

      // read float
      System.out.println(dis.readFloat());

      // read double
      System.out.println(dis.readDouble());

      // read character
      System.out.println(dis.readChar());

      // read boolean
      System.out.println(dis.readBoolean());

      // read String
      System.out.println(dis.readUTF());

      // read last values
      byte[] b = new byte[100];
      System.out.println(dis.read(b));
      for(int i=0; i<b.length; i++) {
         System.out.print((char)b[i]);
      }

      // close stream
      dis.close();
   }
}

Output:-

97
100
99.9
95.5
a
true
knowprogram.com
38
Hello, world!Java program

Limitations of Java DataInputStream class

There are many limitations of the DataInputStream class. Generally, DataInputStream class is used with the combination of DataOutputStream class. We have already discussed limitations in the DataOutputStream class post.

If the data is written as int type i.e. only 4 bytes then we can’t read it using readLong() which needs 8 bytes, because 8 bytes are not available. Hence it will throw EOFException. EOF => End-Of-File.

While working with DataInputStream, and DataOutputStream classes whatever writeXxx() method called in the same order readXxx() method must be called. If the order is not the same then we will get either the wrong result or EOFException. We have to first write the data using DataOutputStream then only we can read them using DataInputStream class.

Without writing primitive data in binary format using DataOutputStream class, we can’t call readXxx() methods of DataInputStream class. Both classes must be used in combination.

If we creating a file and saving data “0061” in the file, now if we try to read these value using readXxx() method then it will give some wrong result because whenever are typing characters to the file, and it won’t be treated as a byte. They are characters, not the bytes.

Let us assume, we have a file and it contains some bytes generated by DataOutputStream. It is “4058 4000 0000 0000” which is written by writeDouble(97) method. Now, this data will be read by readXxx() methods are,

readByte(); // 64
readShort(); // 16472
readInt(); // 1079525376
readLong(); // 4636526185122103296
readFloat(); // 3.3789062
readDouble(); // 97.0
readBoolean(); // true
readUTF(); // EOFException

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *