Open In App

Java.io.DataOutputStream in Java

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Syntax: 

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

Syntax:

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. 

Syntax:

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

Parameters: 

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

Return Type: void 

Method 4: 

Syntax:

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

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

Parameters:

  • 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. 

Syntax:

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.

Syntax:

void writeBoolean (boolean v) 

Return Type: Void

Parameters: Boolean value

Method 7: writeBoolean()

Syntax:

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. 

Syntax:

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.

Syntax:

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. 

Syntax:

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. 

Syntax:

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. 

Syntax:

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.

Example:

Java




// Java Program to Demonstrate DataOutputStream
  
// Importing required classes
import java.io.*;
  
// 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")) ) {
                        dout.writeDouble(1.1);
                        dout.writeInt(55);
                        dout.writeBoolean(true);
                        dout.writeChar('4');
                }
  
                catch (FileNotFoundException ex) {
                        System.out.println("Cannot Open the Output File");
                        return;
                }
  
                // 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");
                        return;
                }
        }
}


Output:

Values: 1.1 55 true 4

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads