When you need to process primitive types, Java provides DatalnputStream and DataOutputStream classes.
The DatalnputStream class is a subclass of FilterlnputStream that reads bytes from a stream and converts them into appropriate primitive type values or strings.
The DatalnputStream class implements the Datalnput interface that defines the methods for reading primitive types values or strings from a byte input stream.
import java.io.*;
class DataIOStreamExample
{
public static void main(String[] args)
{
try
{
FileOutputStream fin=new FileOutputStream ("Std.dat");
DataOutputStream din = new DataOutputStream(fin);
din.writeInt(101);
din.writeUTF("Dinesh Thakur");
din.writeDouble(99.5);
din.writeLong(98765432);
din.close();
}
catch(IOException e) { e.printStackTrace();}
try
{
FileInputStream fin=new FileInputStream("std.dat");
DataInputStream din=new DataInputStream(fin);
System.out.println("Roll Number : " + din.readInt());
System.out.println("Name : "+din.readUTF());
System.out.println("Percentage : "+ din.readDouble());
System.out.println("Phone Number :"+ din.readLong());
din.close();
}
catch(IOException e) { e.printStackTrace(); }
}
}