File Handling is vital in programming to integrate the sorted records in the system with the code to edit, or update the records conveniently. In Java, this functionality can be achieved via the “DataOutputStream” and “DataInputStream” classes that assist the developer in exporting or writing the records to the files and then importing or reading them, respectively.
This blog discusses using “java.io.DataOutputStream” in Java.
How to Use java.io.DataOutputStream in Java?
The “DataOutputStream” class makes a new data output stream for writing data to the target output stream. This stream enables an application to write primitive data types to output streams in a portable manner.
“DataOutputStream” Class Methods
Method | Functionality |
write() | This method writes specified bytes to the output stream. |
writeBoolean(boolean a) | It writes Boolean to output stream in the form of a 1-byte value. |
writeChar(int a) | This method writes a character to the output stream in the form of a 2-byte value. |
writeChars(String st) | It writes strings to the output stream in the form of a combination of characters. |
writeInt(int a) | It writes an integer to the output stream. |
writeLong(long a) | This method writes a long value to the output stream. |
writeByte(int a) | This method writes a byte value to the output stream in the form of a 1-byte value. |
writeBytes(String st) | It writes string(s) to the output stream in the form of a byte combination. |
writeUTF(String str) | It writes string(s) to the output stream via the UTF-8 encoding in a portable way. |
flush() | It flushes the data output stream. |
Now, proceed to the practical implementation of the Java “DataOutputStream” class.
Example 1: Utilizing the “java.io.DataOutputStream” Class in Java
This example uses the “DataOutputStream” class method to write data to a text file:
import java.io.*;
public class Outputstream {
public static void main(String args[]) throws IOException {
try ( DataOutputStream object1 =new DataOutputStream(new FileOutputStream("rdfile.txt")) ) {
object1.writeInt(66);
object1.flush();
System.out.println("Data Written Successfully!");
}
catch (FileNotFoundException ex) {
System.out.println("Can't Open the Output File");
return;
}
}}
In this code:
- Firstly, import the “java.io.*” package to work with the input/output streams.
- Also, declare the “IOException” via the “throws” keyword.
- In the “try” block, create a “DataOutputStream” object that writes to the “rdfile.txt” file.
- Now, apply the “writeInt()” method to write the stated integer to the file and flush the stream.
- In the “catch” block, specify the probable exception/limitation i.e., “FileNotFoundException” to cope with it if faced.
Output

Written File

Example 2: Utilizing the “java.io.DataOutputStream” Class With the “java.io.DataInputStream” Class in Java
This example applies the “DataOutputStream” class to write to a file and then utilizes the “DataInputStream” class to read from that file as well:
import java.io.*;
public class Outputstream {
public static void main(String args[]) throws IOException {
try ( DataOutputStream object1 =new DataOutputStream(new FileOutputStream("rdfile.txt")) ) {
object1.writeDouble(2.1);
object1.writeInt(4);
object1.writeBoolean(true);
object1.writeChar('a');
}
catch (FileNotFoundException ex) {
System.out.println("Can't Open the Output File");
return;
}
try ( DataInputStream din =new DataInputStream(new FileInputStream("rdfile.txt")) ) {
double db = din.readDouble();
int in = din.readInt();
boolean bo = din.readBoolean();
char ch = din.readChar();
System.out.println("Read Values -> \n" + db + "\n" + in + "\n" + bo + "\n" +ch);
}
catch (FileNotFoundException exc) {
System.out.println("Can't Open the Input File");
return;
}
}}
In this snippet of code:
- Likewise, create a “DataOutputStream” class object that writes to the specified text file.
- After that, in the “try” block, apply the “writeDouble()”, “writeInt()”, “writeBoolean()”, and “writeChar()” methods to write the double, integer, boolean, and character values, respectively to the file.
- In the “catch” block, cope with the discussed probable exception.
- Now, create a “DataInputStream” object to read from the written file.
- Apply the “readDouble()”, “readInt()”, “readBoolean()”, and “readChar()” methods to read the written values of the corresponding data types.
- Finally, in the “catch” block, handle the discussed exception.
Output

From this output, it can be implied that the written values in the file are read appropriately.
Conclusion
The “DataOutputStream” class creates a new data output stream for writing data to the specified output stream via its methods such as “writeInt()”, “writeDouble()” etc. This guide has explained the utilization of the Java “DataOutputStream” class.