How do I use DataInputStream and DataOutputStream?

DataOutputStream and DataInputStream give us the power to write and read primitive data type to a media such as file. Both of this class have the corresponding method to write primitive data and read it back.

Using this class make it easier to read integer, float, double data and others without needing to interpret if the read data should be an integer or a float data. Let’s see our code below.



public class PrimitiveStreamExample {

    public static void main(String[] args) {
        // Prepares some data to be written to a file.
        int cityIdA = 1;
        String cityNameA = "Green Lake City";
        int cityPopulationA = 500000;
        float cityTempA = 15.50f;

        int cityIdB = 2;
        String cityNameB = "Salt Lake City";
        int cityPopulationB = 250000;
        float cityTempB = 10.45f;

        try {
            // Create an instance of FileOutputStream with cities.dat
            // as the file name to be created. Then we pass the input
            // stream object in the DataOutputStream constructor.
            FileOutputStream fos = new FileOutputStream("cities.dat");
            DataOutputStream dos = new DataOutputStream(fos);

            // Below we write some data to the cities.dat.
            // DataOutputStream class have various method that allow
            // us to write primitive type data and string. There are
            // method called writeInt(), writeFloat(), writeUTF(),
            // etc.



            // Now we have a cities.dat file with some data in it.
            // Next you'll see how easily we can read back this
            // data and display it. Just like the DataOutputStream
            // the DataInputStream class have the corresponding
            // read methods to read data from the file. Some of
            // the method names are readInt(), readFloat(),
            // readUTF(), etc.
            FileInputStream fis = new FileInputStream("cities.dat");
            DataInputStream dis = new DataInputStream(fis);

            // Read the first data
            int cityId1 = dis.readInt();
            System.out.println("Id: " + cityId1);
            String cityName1 = dis.readUTF();
            System.out.println("Name: " + cityName1);
            int cityPopulation1 = dis.readInt();
            System.out.println("Population: " + cityPopulation1);
            float cityTemperature1 = dis.readFloat();
            System.out.println("Temperature: " + cityTemperature1);

            // Read the second data
            int cityId2 = dis.readInt();
            System.out.println("Id: " + cityId2);
            String cityName2 = dis.readUTF();
            System.out.println("Name: " + cityName2);
            int cityPopulation2 = dis.readInt();
            System.out.println("Population: " + cityPopulation2);
            float cityTemperature2 = dis.readFloat();
            System.out.println("Temperature: " + cityTemperature2);
        } catch (IOException e) {

The generated result of our program are:

City Id: 1
City Name: Green Lake City
City Population: 500000
City Temperature: 15.5

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.


  1. Hi Bhavani,

    The output file, cities.dat, will be create in the path where you executed the example code. You can also change the constructor call in FileOutputStream above with a complete path where you want to save the output. Or instead of using string, you can also pass an instance of with a complete path and filename to FileOutputStream.

Leave a Reply