Open In App
Related Articles in Java

Improve Article
Save Article
Like Article

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. Let us do discuss the constructor of this class prior to moving ahead to the methods of this class. 

Constructor: DataOutputStream (OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream. Now let us discuss important methods of this class prior to implementing the same. 

Method 1: void flush() 

Flushes this data output stream. 


public void flush() throws IOException

Note: It do overrides the flush() method in class FilterOutputStream

Exception thrown: IOException

Method 2: size()

Returns the current value of the counter written, the number of bytes written to this data output stream so far


public final int size()

Return Type: An integer value of the written field

Method 3: write () 

Writes len bytes from the specified byte array starting at offset off to the underlying output stream. 


void write (byte[] b, int off, int len)


  • Byte array
  • int off
  • Length of the byte array

Return Type: void 

Method 4: 


public void write(byte[] b, int off,int len)

Note: It overrides write() method in FilterOutputStream class.


  • The data
  • The start offset in the data
  • The number of bytes to write

Exceptions: IOException

Method 5: void write (int b): 

Writes the specified byte (the low eight bits of argument b) to the underlying output stream. 


public void write(int b)
throws IOException

ReturnType: Void

Parameters:  The byte to be written

Method 6: writeBoolean()

Writes a boolean to the underlying output stream as a 1-byte value.


void writeBoolean (boolean v) 

Return Type: Void

Parameters: Boolean value

Method 7: writeBoolean()


public final void writeBoolean(boolean v)
throws IOException 

Parameters: Boolean value to be written.

Throws: IOException

Method 8: writeByte()

Writes out a byte to the underlying output stream as a 1-byte value. 


public final void writeByte(int v)
throws IOException 

Parameters: A byte value to be written 

Exception Thrown:  IOException

Method 9: writeChar()

Writes a char to the underlying output stream as a 2-byte value, high byte first.


public final void writeChar(int v)
throws IOException

Parameters:  A character value to be written.

Exception: IOException 

Method 10: void writeDouble (double v)

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. 


public final void writeDouble(double v)
throws IOException

Parameters: A double value to be written

Exception thrown: IOException

Method 11: writeFloat (float v)

Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, MSB first. 


public final void writeFloat(float v)
throws IOException

Return Type: Void

Parameters: A float value to be written.

Exception thrown: IOException

Method 12: writeInt()

Writes an int to the underlying output stream as four bytes, high byte first. 


public final void writeInt(int v)
throws IOException

Parameters: An int to be written.

Return Type: Void

Exceptions Thrown: Writes an int to the underlying output stream as four bytes, high byte first. 

Method 13: writeLong()

Writes a long to the underlying output stream as eight bytes, high byte first. 

Syntax :

public final void writeLong(long v)
throws IOException

Parameters:  Long to be written.

Throws: IOException

Method 14: writeShort(): 

Writes a short to the underlying output stream as two bytes, high byte first. 

Return Type: Void

Parameter: A short to be written

Syntax :

public final void writeShort(int v)
throws IOException

Method 15: writeUTF (String str) 

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner. 

Parameter: A string to be written

Return Type:  Void 

Exception Thrown: IOException

Note: There are certain important points to be remembered as listed: 

  • DataOutputStream and DataInputStream are often used together.
  • When a DataOutputStream is closed (by calling close( )), the underlying stream specified by out is also closed automatically.
  • There is no longer any explicit close() method call. The try-with-resources construct takes care of that.



// Java Program to Demonstrate DataOutputStream
// Importing required classes
// Main class
// DataOutputStreamDemo
class GFG {
        // Main driver method
        public static void main(String args[]) throws IOException {
                // Try block to check for exceptions
                // Writing the data using DataOutputStream
                try ( DataOutputStream dout =
                                                new DataOutputStream(new FileOutputStream("file.dat")) ) {
                catch (FileNotFoundException ex) {
                        System.out.println("Cannot Open the Output File");
                // Reading the data back using DataInputStream
                try ( DataInputStream din =
                                                new DataInputStream(new FileInputStream("file.dat")) ) {
                        double a = din.readDouble();
                        int b = din.readInt();
                        boolean c = din.readBoolean();
                        char d = din.readChar();
                        System.out.println("Values: " + a + " " + b + " " + c + " " + d);
                // Catch block to handle FileNotFoundException
                catch (FileNotFoundException e) {
                        System.out.println("Cannot Open the Input File");


Values: 1.1 55 true 4

Note: The above program uses try-with-resources so do it require JDK 7 or later.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 Sep, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials