Data Types Read Write Java

DataInputStream includes many methods like readByte(), readInt(), readLong(), readFloat(), readDouble() and readUTF() etc with which a byte, an integer, a long, a float, a double and a word can be read as a whole. Similarly, the DataOutputStream includes the methods like writeByte(), writeInt(), writeLong(), writeFloat(), writeDouble() and writeUTF() that can write a byte, an integer, a long, a float, a double and a word at a time. In the following program, these methods are used.

Two programs exist – WriteDataTypes.java that writes to the file EveryThing.txt and ReadDataTypes.java that reads the values from the file and print them.

Data Types Read Write Java: The following program writes data types like int, double, long etc. into the file "EveryThing.txt".
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class WriteDataTypes
{
  public static void main(String args[]) throws IOException
  {
    byte b = 10;
    int    x = 50;
    long m = 100;
    float price = 10.5f;
    double rate = 20.6;
    String str1 = "Hello";

    FileOutputStream fos = new FileOutputStream("EveryThing.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    dos.writeByte(b);          dos.writeInt(x); 
    dos.writeLong(m);          dos.writeFloat(price);
    dos.writeDouble(rate);     dos.writeUTF(str1);

    dos.close();  fos.close();
  }
}

The writeXXX() methods write the appropriate data type to the file, EveryThing.txt. The above two streams can be replaced as follows using anonymous objects that saves memory.

DataOutputStream dos = new DataOutputStream(new FileOutputStream("EveryThing.txt"));

The data written by writeXXX() methods in EveryThing.txt is not in a readable format (you can open and see how the file contents look). It must be read by readXXX() methods of DataInputStream as in the next program.

Following program reads data types from the file "”"EveryThing.txt"”" (written earlier in the previous program).

Data Types Read Write Java: The following program reads the data and prints at the DOS prompt.
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class ReadDataTypes
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fis = new FileInputStream("EveryThing.txt");
    DataInputStream dis = new DataInputStream(fis);

    System.out.println("The byte value: " + dis.readByte());   
    System.out.println("The int value: " + dis.readInt());   
    System.out.println("The long value: " + dis.readLong());   
    System.out.println("The float value: " + dis.readFloat());   
    System.out.println("The double value: " + dis.readDouble());   
    System.out.println("The word: " + dis.readUTF());   

    dis.close();  fis.close();
   }
}

Data Types Read Write Java
Output screen on Data Types Read Write Java

The readXXX() methods read the appropriate data type from the file and prints them. The two streams can be replaced as follows using anonymous objects to save memeory.

DataInputStream dis = new DataInputStream(new FileInputStream("EveryThing.txt"));

Leave a Comment

Your email address will not be published.