writing-and-reading-data-in-file-using-DataInputStream-and-DataOutputStream-in-java-with-examples

admin

2/27/2025

Java file operations DataInputStream vs DataOutputStream

Go Back

Writing and Reading Data in File Using DataInputStream and DataOutputStream in Java

Updated: 06/Feb/2025 by Computer Hope

Efficient file handling is crucial for Java developers working with structured data storage. The DataInputStream and DataOutputStream classes allow reading and writing primitive data types efficiently. This tutorial provides a detailed guide on how to write and read data from a file using Java.

Java file operations DataInputStream vs DataOutputStream

Why Use DataInputStream and DataOutputStream?

  • Enables structured binary file storage.
  • Reads and writes Java primitive types (int, double, UTF strings, etc.).
  • Ensures data consistency during file operations.
  • Useful for handling serialized objects.

1. Writing Data to a File Using DataOutputStream

The DataOutputStream class provides methods to write primitive data types to a file.

Example Code to Write Data to a File

import java.io.*;
import java.util.*;

class Emp {
    private int age;
    private String name;
    private double sal;

    public void get() {
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter age, name, and salary:");
        age = kb.nextInt();
        name = kb.next();
        sal = kb.nextDouble();
    }

    public void show() {
        System.out.println("Age: " + age);
        System.out.println("Name: " + name);
        System.out.println("Salary: " + sal);
    }

    public void writeInFile() {
        try (DataOutputStream dout = new DataOutputStream(new FileOutputStream("D:/DeveloperFile.dat"))) {
            dout.writeInt(age);
            dout.writeUTF(name);
            dout.writeDouble(sal);
        } catch (Exception ex) {
            System.out.println("Error in writing: " + ex);
        }
    }
}

Output (Writing to File)

Enter age, name, and salary:
25 John 55000.50
Data written successfully.

2. Reading Data from a File Using DataInputStream

The DataInputStream class allows reading primitive data types from a file in the same order they were written.

Example Code to Read Data from a File

class ExampleReadWrite {
    public static void main(String args[]) {
        Emp E = new Emp();
        E.get();
        E.writeInFile();
        
        Emp F = new Emp();
        F.readFromFile();
        F.show();
    }
}

Output (Reading from File)

Age: 25
Name: John
Salary: 55000.50

3. Handling File Data Efficiently

By using DataInputStream and DataOutputStream, developers can:

  • Preserve data types when writing and reading.
  • Prevent file corruption by using structured data storage.
  • Reduce conversion errors compared to using BufferedReader or PrintWriter.

Best Practices for File Handling in Java

  • Always close file streams after use.
  • Use exception handling to prevent crashes during I/O operations.
  • Ensure the correct sequence of writing and reading data.

Conclusion

DataOutputStream and DataInputStream are essential for structured binary data handling in Java. By following best practices, developers can ensure efficient file management and data integrity.

Table of content