Data Input and Data Output Stream

Filter Course


Data Input and Data Output Stream

Published by: Dikshya

Published date: 25 Jun 2023

Data Input and Data Output Stream

Data Input Stream and Data Output Stream are higher-level stream classes that provide additional functionality for reading and writing specific types of data, such as primitive data types (e.g., int, double) or objects. They are commonly used in conjunction with File Input Stream and File Output Stream to read and write formatted data from files.

Data Input Stream:

A Data Input Stream is a class that provides methods for reading data from an underlying input stream. It allows you to read different types of data, such as integers, floating-point numbers, booleans, characters, and strings, from the input stream. The Data Input Stream ensures that the data is read in a format that is compatible with the data type being read.

Data Output Stream:

A Data Output Stream is a class that provides methods for writing data to an underlying output stream. It allows you to write different types of data, such as integers, floating-point numbers, booleans, characters, and strings, to the output stream. The Data Output Stream ensures that the data is written in a format that can be later read correctly by a Data Input Stream.

Here's an example in Java that demonstrates the usage of Data Input Stream and Data Output Stream in combination with File Input Stream and File Output Stream:

import java.io.*;

public class DataIOExample {
    public static void main(String[] args) {
        try {
            // File Output Stream
            FileOutputStream fileOutputStream = new FileOutputStream("data.bin");
            DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);

            // Write data using Data Output Stream
            dataOutputStream.writeInt(42);
            dataOutputStream.writeDouble(3.14159);
            dataOutputStream.writeBoolean(true);
            dataOutputStream.writeUTF("Hello, World!");

            dataOutputStream.close();

            // File Input Stream
            FileInputStream fileInputStream = new FileInputStream("data.bin");
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);

            // Read data using Data Input Stream
            int intValue = dataInputStream.readInt();
            double doubleValue = dataInputStream.readDouble();
            boolean booleanValue = dataInputStream.readBoolean();
            String stringValue = dataInputStream.readUTF();

            System.out.println("Int Value: " + intValue);
            System.out.println("Double Value: " + doubleValue);
            System.out.println("Boolean Value: " + booleanValue);
            System.out.println("String Value: " + stringValue);

            dataInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, the Data Output Stream is used to write various types of data (integer, double, boolean, string) to the "data.bin" file. Then, the Data Input Stream is used to read the same types of data from the file and display them on the console.