DataInputStream in Java

DataInputStream is filter streams that is used to read strings and primitive data types comprised of more than a single byte. DataInputStream implement the DataInput interface. This interface defines methods for reading strings and all of the Java primitive types, including numbers and Boolean values. 

We can construct a DataInputStream from an InputStream and then use a method such as readLine() to read a String data type:

    DataInputStream dis = new DataInputStream( System.in );
    String ss = dis.readLine();
 
 
Consider the following example which reads primitive data types such as int, double from the text file file2.txt and prints the result.

Example:

/**
From dataInputStream.java
*/

import java.io.DataInputStream;
import java.io.FileInputStream;

/**
 *
 * @author JavaHotSpot
 */

public class dataInputStream
{
 public static void main(String args[])
 {
     try
     {
          int roll;
          char name;
          double percentage;

          FileInputStream fs = new FileInputStream("C:\\file2.txt");
        
          DataInputStream dins = new DataInputStream(fs);
          while(fs.available()>0)
          {
              roll=dins.readInt();
             
              percentage=dins.readDouble();
              System.out.println(roll+"  "+percentage);

          }
     }
     catch(Exception ex)
     {
         System.err.println(ex);
     }
 }
}

Output:
1  98.34
2  97.32
3  89.65
4  87.34
5  78.12