Java.Streams.Which class allows you to read data from an input byte stream in the format of primitive data types?

The class you’re looking for is DataInputStream.


📘 DataInputStream

It allows you to read primitive data types (like int, float, double, long, char, boolean, etc.) from an input byte stream in a machine-independent way.


🧠 Why use it?

When you’re reading raw binary data that was written using DataOutputStream or some binary format — DataInputStream lets you easily read it back as Java primitives.

Constructor

DataInputStream(InputStream in)

You typically wrap it around another stream like FileInputStream.

✅ Example

import java.io.*;

public class ReadData {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("data.bin");
             DataInputStream dis = new DataInputStream(fis)) {

            int number = dis.readInt();
            double value = dis.readDouble();
            boolean flag = dis.readBoolean();

            System.out.println("Int: " + number);
            System.out.println("Double: " + value);
            System.out.println("Boolean: " + flag);

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

🔢 Supported Read Methods

  • readInt()
  • readDouble()
  • readFloat()
  • readBoolean()
  • readLong()
  • readChar()
  • readShort()
  • readUTF() (for reading UTF-8 encoded strings)
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.