Java DataOutputStream Class

Java is a wide range and used efficiently for many purposes. DataOutputStream Classes are used in various applications of Java. It shows various methods for the data output of Stream.

Explanation

  • The Java DataOutputStream class enables machine-independent writing of primitive Java data types to the output stream by applications.
  • Data that a data input stream can subsequently read is often written by Java applications using the data output stream.

Declaration

public class DataOutputStream extends
FilterOutputStream implements DataOutput

Class Methods:

Method  Description
int size() It returns the number of bytes written to the data output stream.
void write(int b) It is used to write the specified byte to the underlying output stream.
void write(byte[] b, int off, int len) It can be used to write Len bytes of data to the output stream.
void writeBoolean(boolean v) It writes Boolean values to the output stream as 1-byte values.
void writeChar(int v) It is employed to write a 2-byte value representing char to the output stream.
void writeChars(String s) It writes a string as a series of characters to the output stream.
void writeByte(int v) It is employed to write a single byte as a value of one byte to the output stream.
void writeBytes(String s) It is employed to write a string as a series of bytes to the output stream.
void writeInt(int v) An int is written to the output stream using it.
void writeShort(int v) It is employed for writing briefs to the output stream.
void writeLong(long v) Writing a long to the output stream is done with it.

Fields:

Method Description
Protected int Written

The total number of bytes written to the output stream thus far.

Example:

package com.java;  
import java.io.*;  
public class TechVidvan{  
    public static void main(String[] args)  {  
        FileOutputStream file = new FileOutputStream(D:\\testout.txt);  
        DataOutputStream data = new DataOutputStream(file);  
        data.writeInt(65);  
        data.flush();  
        data.close();  
        System.out.println("Java Learner in TechVidvan");  
    }  
}

Output:
Java Learner in TechVidvan

Example 2

import java.io.*; 

class TechVidvan{ 
                public static void main(String args[]) throws IOException { 
                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("File not found"); 
                        return; 
                } 
  
                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 (FileNotFoundException e) { 
                        System.out.println("file not found"); 
                        return; 
                } 
        } 
}

Output:
Values: 1.1 55 true 4

Class Constructors:

DataOutputStream with out
output stream of out

DataOutputStream(OutputStream out)

  • By doing this, a new data output stream is created and data is written to the designated underlying output stream.
  • A new data output stream is created to write data to the designated underlying output stream. The printed counter has a zero value.

Parameters: out: the output stream being used, which can be saved for later use.

Fields:

  • protected int written: This is the total amount of bytes written to the data output stream thus far.
  • protected OutputStream out: This is the stream of output that has to be filtered.

Uses:

  • It is common practice to combine DataOutputStream and DataInputStream.
  • The underlying stream indicated by out is automatically closed when a DataOutputStream is closed (by calling close()).
  • The close() method is no longer explicitly called. The try-with-resources handle that construct.

Importance of DataOutputStream:

  • It is common practice to combine DataOutputStream and DataInputStream.
  • The underlying stream indicated by out is automatically closed when a DataOutputStream is closed (by calling close()).
  • The close() method is no longer explicitly called. The try-with-resources handle that construct.

Conclusion

The DataOutputStream is used in various areas in Java. Many examples are used for data output in a detailed manner.