Java I/O Data Streams

Data streams in Java are used to read and write primitive data types (such as int, float, double, etc.) and String values in a machine-independent way. They provide methods for input and output of these data types, ensuring that the data can be read back in the same way it was written, regardless of the platform.

Table of Contents

  1. Introduction
  2. Data Stream Classes
    • DataInputStream
    • DataOutputStream
  3. Examples
    • Writing Data to a File
    • Reading Data from a File
    • Copying Data Using Data Streams
  4. Conclusion

Introduction

Data streams are designed for reading and writing primitive data types and strings in a binary format. The primary classes for data streams are DataInputStream and DataOutputStream. These classes work with underlying byte streams (InputStream and OutputStream) to handle the input and output of data in a binary format.

Data Stream Classes

DataInputStream

The DataInputStream class allows an application to read primitive data types from an underlying input stream in a machine-independent way.

Common Methods:

  • readBoolean()
  • readByte()
  • readChar()
  • readDouble()
  • readFloat()
  • readInt()
  • readLong()
  • readShort()
  • readUTF()

DataOutputStream

The DataOutputStream class allows an application to write primitive data types to an output stream in a machine-independent way.

Common Methods:

  • writeBoolean(boolean v)
  • writeByte(int v)
  • writeChar(int v)
  • writeDouble(double v)
  • writeFloat(float v)
  • writeInt(int v)
  • writeLong(long v)
  • writeShort(int v)
  • writeUTF(String s)

Examples

Writing Data to a File

The following example demonstrates how to write primitive data types and a string to a file using DataOutputStream.

Example

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamExample {
    public static void main(String[] args) {
        try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"))) {
            dos.writeInt(123);
            dos.writeDouble(45.67);
            dos.writeBoolean(true);
            dos.writeUTF("Hello, World!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • DataOutputStream is used to write data to a file named data.dat.
  • The writeInt(), writeDouble(), writeBoolean(), and writeUTF() methods write the respective data types to the file.

Reading Data from a File

The following example demonstrates how to read the data written in the previous example using DataInputStream.

Example

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

public class DataInputStreamExample {
    public static void main(String[] args) {
        try (DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"))) {
            int intValue = dis.readInt();
            double doubleValue = dis.readDouble();
            boolean booleanValue = dis.readBoolean();
            String stringValue = dis.readUTF();

            System.out.println("Read values: " + intValue + ", " + doubleValue + ", " + booleanValue + ", " + stringValue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • DataInputStream is used to read data from a file named data.dat.
  • The readInt(), readDouble(), readBoolean(), and readUTF() methods read the respective data types from the file.
  • The values are printed to the console.

Copying Data Using Data Streams

The following example demonstrates how to copy data from one file to another using DataInputStream and DataOutputStream.

Example

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStreamCopyExample {
    public static void main(String[] args) {
        try (DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"));
             DataOutputStream dos = new DataOutputStream(new FileOutputStream("copy.dat"))) {
            int intValue = dis.readInt();
            double doubleValue = dis.readDouble();
            boolean booleanValue = dis.readBoolean();
            String stringValue = dis.readUTF();

            dos.writeInt(intValue);
            dos.writeDouble(doubleValue);
            dos.writeBoolean(booleanValue);
            dos.writeUTF(stringValue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • DataInputStream reads data from data.dat.
  • DataOutputStream writes data to copy.dat.
  • The data read from the input file is written to the output file, effectively copying the data.

Conclusion

Data streams in Java provide a way to read and write primitive data types and strings in a binary format. They ensure that data can be read back in the same way it was written, regardless of the platform. By understanding how to use DataInputStream and DataOutputStream, you can efficiently manage and process binary data in your Java applications. The examples provided demonstrate basic usage, including writing data to a file, reading data from a file, and copying data using data streams.

Comments