Java DataOutputStream Class


DataOutputStream allows you to write primitive data to the output stream. It implements the DataOutput interface. DataOutput interface defines a method to convert primitive values into a sequence of bytes.

Syntax:

public class DataOutputStream extends FilterOutputStream implements DataOutput 

To create an OutputStream

DataOutputStream out = DataOutputStream(OutputStream out); 

DataOutputStream Class Methods

S.No Method Description
1 Public final int write(byte [] b) It is used to writes the current number of bytes written to this data output stream.
2 void writeByte(int m) It is used to write a byte to the output stream
3 public final void write(byte[] w, int off, int len) It is used to write len bytes of data to the output stream.
4 public final void writeBoolean(boolean b) It is used to write Boolean to the output stream as a 1-byte value.
5 Public final void writeBytes(String s) It is used to write string to the output stream as a sequence of bytes.
6 Public void flush() It is used to flushes the data output stream.

Example:

import java.io.*;
public class DataOutputStreamDemo {
    public static void main(String args[]) throws IOException{  
		FileOutputStream fout = new FileOutputStream("Out.txt");
		DataOutputStream out = new DataOutputStream(fout);
		out.writeInt(100);
    	out.flush();  
        out.close();  
        System.out.println("Success"); 

    }
} 

Output

 Success

Out.txt

100 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.