DataOutputStream in Java



DataOutputStream File Java

What is Java DataOutputStream

The DataOutputStream class in Java allows us to write primitive data type values to an output stream. This operation is machine-independent and portable. We can use any output stream class like FileOutputStream which is an argument to the DataOutputStream constructor.

DataOutputStream dos = new DataOutputStream(OutputStream out);

DataOutputStream in Java

Java DataOutputStream class methods

Below are the methods that the DataOutputStream class supports.

MethodsDescription
void close()Closes the output stream
void flush()Flushes the data in the output stream and forces any buffered data to be written to the file
int size()Returns the current number of bytes in the output stream
void write(byte[] b)Writes all the bytes in the array b to the output stream
void write(int b)Writes the specified byte to the output stream
void write(byte[] b, int off, int len)Writes the array of bytes of specified length starting from the offset position
void writeBoolean(boolean b)Writes the specified boolean value to the output stream
void writeByte(int b)Writes the specified byte to the output stream
void writeBytes(String s)Writes the specified string of bytes to the output stream
void writeChar(char c)Writes the specified character to the output stream
void writeChars(String s)Writes the specified string of characters to the output stream
writeDouble(double d)Writes the specified double value to the output stream
writeFloat(float f)Writes the specified float value to the output stream
writeInt(int i)Writes the specified integer value to the output stream
writeLong(long l)Writes the specified long value to the output stream
writeShort(short s)Writes the specified short value to the output stream
void writeUFT(String s)Writes the specified string in the form of unicode character to the output stream
OutputStream nullOutputStream()Returns a new OutputStream by discarding all the bytes

Java DataOutputStream Example

Below is an example to illustrate the implementation of various Java DataOutputStream class methods. There are separate methods to write values of different data types. For eg: writeInt for writing integers, writeChar for a character, writeDouble for double, writeFloat for float, writeBoolean for boolean, writeUFT for a string in Unicode format, writeLong for writing long numbers, writeByte for Byte values, writeChars for writing characters as a string, etc.

The size() method retrieves the number of bytes in the file.

Also, whenever we use the close() method to close the DataOutputStream, it automatically closes the underlying inner stream(FileOutputStream). Hence we don’t need to explicitly close it.

import java.io.*;

public class WriteFile {

  public static void main(String[] args) throws IOException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    DataOutputStream dos = new DataOutputStream(fo);
    
    String text = "This is an example of DataOutputStream";
    byte[] b = text.getBytes();
    int i = 100;
    double d = 123.45;
    float f = 45.5f;
    boolean bl = true;
    
    dos.writeUTF("Write Unicode");
    dos.writeInt(i);
    dos.writeChars("Write character");
    dos.writeByte(75);
    dos.writeLong(999999);
    dos.writeBoolean(bl);
    dos.writeFloat(f);
    dos.writeDouble(d);
    dos.writeChar('d');
    
    System.out.println("Number of bytes written: " + dos.size());
    System.out.println("Success");
    
    dos.close();
  }

}
Number of bytes written: 73
Success

We usually use both DataOutputStream and DataInputStream together to write and read the data correspondingly. Whenever we want to write large bytes of data to the output stream, we use the DataOutputStream class and we can ensure to read it back in the right order.

In the below example we can see how to write using the Java DataOutputStream and correspondingly read integer, UFT, and character values using the DataInputStream.

import java.io.*;

public class WriteFile {

  public static void main(String[] args) throws IOException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    DataOutputStream dos = new DataOutputStream(fo);
    
    dos.writeInt(60);
    dos.writeUTF("Welcome");
    dos.writeChar('c');
    System.out.println("Number of bytes written: " + dos.size());
    
    FileInputStream fi = new FileInputStream("OutputFile.txt");
    DataInputStream di = new DataInputStream(fi);
    
    System.out.println(di.readInt());
    System.out.println(di.readUTF());
    System.out.println(di.readChar());
    
    System.out.println("Success");
    
    dos.close();
  }

}
Number of bytes written: 15
60
Welcome
c
Success

 

Reference

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions