How to Read a File in Java Using DataInputStream

Reading a file in Java can be accomplished using various classes from the java.io package. One such class is DataInputStream, which allows an application to read primitive data types from an underlying input stream in a machine-independent way. This blog post will guide you through the process of reading a file using DataInputStream.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Using DataInputStream to Read a File
  4. Complete Example
  5. Conclusion

Introduction

DataInputStream is a class in the java.io package that lets an application read Java primitive data types from an underlying input stream in a machine-independent way. It is particularly useful when reading data that was written using a DataOutputStream. This tutorial will demonstrate how to use DataInputStream to read various data types from a file.

Prerequisites

Before you start, ensure you have the following:

  • A basic understanding of Java programming
  • A Java development environment (JDK and an IDE like IntelliJ IDEA or Eclipse)

Using DataInputStream to Read a File

To read a file using DataInputStream, you will:

  1. Create a FileInputStream to read the file.
  2. Wrap the FileInputStream with a DataInputStream.
  3. Read the data using the DataInputStream.
  4. Close the streams to release system resources.

Example

The following example demonstrates how to read a file using DataInputStream.

Step 1: Writing Data to a File

First, let's write some data to a file using DataOutputStream.

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

public class WriteDataToFile {
    public static void main(String[] args) {
        String fileName = "datafile.dat";

        try (FileOutputStream fos = new FileOutputStream(fileName);
             DataOutputStream dos = new DataOutputStream(fos)) {

            dos.writeUTF("Hello, World!");
            dos.writeInt(123);
            dos.writeDouble(45.67);

            System.out.println("Data has been written to " + fileName);

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

Explanation:

  • A FileOutputStream is created to write to a file named datafile.dat.
  • A DataOutputStream is created to write data types to the FileOutputStream.
  • The writeUTF, writeInt, and writeDouble methods are used to write a String, an int, and a double to the file.
  • The try-with-resources statement ensures that the streams are closed automatically.

Step 2: Reading Data from a File

Now, let's read the data from the file using DataInputStream.

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

public class ReadDataFromFile {
    public static void main(String[] args) {
        String fileName = "datafile.dat";

        try (FileInputStream fis = new FileInputStream(fileName);
             DataInputStream dis = new DataInputStream(fis)) {

            String str = dis.readUTF();
            int intValue = dis.readInt();
            double doubleValue = dis.readDouble();

            System.out.println("Read String: " + str);
            System.out.println("Read int: " + intValue);
            System.out.println("Read double: " + doubleValue);

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

Explanation:

  • A FileInputStream is created to read the file named datafile.dat.
  • A DataInputStream is created to read data types from the FileInputStream.
  • The readUTF, readInt, and readDouble methods are used to read a String, an int, and a double from the file.
  • The try-with-resources statement ensures that the streams are closed automatically.

Complete Example

Here is the complete example, including both writing to and reading from the file.

WriteDataToFile.java

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

public class WriteDataToFile {
    public static void main(String[] args) {
        String fileName = "datafile.dat";

        try (FileOutputStream fos = new FileOutputStream(fileName);
             DataOutputStream dos = new DataOutputStream(fos)) {

            dos.writeUTF("Hello, World!");
            dos.writeInt(123);
            dos.writeDouble(45.67);

            System.out.println("Data has been written to " + fileName);

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

ReadDataFromFile.java

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

public class ReadDataFromFile {
    public static void main(String[] args) {
        String fileName = "datafile.dat";

        try (FileInputStream fis = new FileInputStream(fileName);
             DataInputStream dis = new DataInputStream(fis)) {

            String str = dis.readUTF();
            int intValue = dis.readInt();
            double doubleValue = dis.readDouble();

            System.out.println("Read String: " + str);
            System.out.println("Read int: " + intValue);
            System.out.println("Read double: " + doubleValue);

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

Conclusion

Reading a file in Java using DataInputStream is an efficient way to handle binary data and primitive data types. By using the DataInputStream, you can read data types that were written using a DataOutputStream. The examples provided demonstrate how to write and read data types to and from a file, making it easier to manage binary data in your Java applications.

Feel free to modify and experiment with the code examples provided in this tutorial to suit your specific needs. Happy coding!

Comments