Site icon TechVidvan

Java DataOutputStream Class

java dataoutputstream

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

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)

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

Fields:

Uses:

Importance of DataOutputStream:

Conclusion

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

Exit mobile version