Course Content
Core Java
About Lesson

Data streams support binary I/O of primitive data type values (booleancharbyteshortintlongfloat, and double) as well as String values. All data streams implement either the [DataInpu](<https://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html>)t interface or the DataOutput interface.

Binary I/O: Streams read and write data in binary format, which is more efficient and allows for more control over data.

Common classes of DataStreams are

  1. DataInputStream
  2. DataOutputStream

DataInputStream

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.

Constructor

DataInputStream(InputStream in): Creates a DataInputStream that uses the specified underlying InputStream.

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

public class DataInputExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("/Users/praful/Desktop/datafile1.bin");
             DataInputStream dis = new DataInputStream(fis)) {

            // Read the data in the same order it was written
            boolean booleanValue = dis.readBoolean();
            byte byteValue = dis.readByte();
            char charValue = dis.readChar();
            short shortValue = dis.readShort();
            int intValue = dis.readInt();
            long longValue = dis.readLong();
            float floatValue = dis.readFloat();
            double doubleValue = dis.readDouble();
            String utfString = dis.readUTF();

            // Print the read values
            System.out.println("Boolean value: " + booleanValue);
            System.out.println("Byte value: " + byteValue);
            System.out.println("Char value: " + charValue);
            System.out.println("Short value: " + shortValue);
            System.out.println("Int value: " + intValue);
            System.out.println("Long value: " + longValue);
            System.out.println("Float value: " + floatValue);
            System.out.println("Double value: " + doubleValue);
            System.out.println("UTF String: " + utfString);

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

Common Methods

readBoolean(): Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.

readByte(): Reads and returns one byte of data from the input stream.

readChar(): Reads two input bytes and returns a char value.

readDouble(): Reads eight input bytes and returns a double value.

readFloat(): Reads four input bytes and returns a float value.

readInt(): Reads four input bytes and returns an int value.

readLong(): Reads eight input bytes and returns a long value.

readShort(): Reads two input bytes and returns a short value.

readUTF(): Reads a string that has been encoded using a modified UTF-8 format.

readFully(byte[] b): Reads some bytes from an input stream and stores them into the buffer array b.

readFully(byte[] b, int off, int len): Reads len bytes from an input stream and stores them into the buffer array b starting at offset off.

DataOutputStream

Constructor

DataOutputStream(OutputStream out): Creates a new data output stream to write data to the specified underlying output stream.

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

public class DataOutputExample {
    public static void main(String[] args) {
        try (FileOutputStream fos = new FileOutputStream("/Users/praful/Desktop/datafile1.bin");
             DataOutputStream dos = new DataOutputStream(fos)) {

            // Writing different types of data
            dos.writeBoolean(true);            // Write a boolean value
            dos.writeByte(100);                // Write a byte value
            dos.writeChar('A');                // Write a char value
            dos.writeShort(30000);             // Write a short value
            dos.writeInt(123456);              // Write an int value
            dos.writeLong(123456789L);         // Write a long value
            dos.writeFloat(12.34F);            // Write a float value
            dos.writeDouble(123.456);          // Write a double value
            dos.writeUTF("Hello, World!");     // Write a UTF-8 string

            // Writing a byte array
            byte[] byteArray = {10, 20, 30, 40, 50};
            dos.write(byteArray);              // Write the whole byte array
            dos.write(byteArray, 1, 3);        // Write a subarray of bytes

            // Writing individual bytes
            for (int i = 0; i < byteArray.length; i++) {
                dos.writeByte(byteArray[i]);
            }

            System.out.println("Data has been written to the file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Common Methods

writeBoolean(boolean v): Writes a boolean value to the underlying output stream as a single byte.

writeByte(int v): Writes a byte to the underlying output stream.

writeChar(int v): Writes a char value to the underlying output stream as two bytes.

writeDouble(double v): Writes a double value to the underlying output stream as eight bytes.

writeFloat(float v): Writes a float value to the underlying output stream as four bytes.

writeInt(int v): Writes an int value to the underlying output stream as four bytes.

writeLong(long v): Writes a long value to the underlying output stream as eight bytes.

writeShort(int v): Writes a short value to the underlying output stream as two bytes.

writeUTF(String str): Writes a string to the underlying output stream using modified UTF-8 encoding.

write(byte[] b): Writes b.length bytes from the specified byte array to this output stream.

write(byte[] b, int off, int len): Writes len bytes from the specified byte array starting at offset off to this output stream.

Scroll to Top