Java Dataoutputstream Class

The Java DataOutputStream class enables a program to write primitive Java data types to the output stream in a manner that is independent of the underlying machine.

In Java programming, data output stream is commonly employed to store data that can be retrieved later by a data input stream.

Java DataOutputStream class declaration

Now, let's examine the declaration of the java.io.DataOutputStream class:

Example

public class DataOutputStream extends FilterOutputStream implements DataOutput

Java DataOutputStream class methods

Method Description
int size() It is used to return 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 is used to write len bytes of data to the output stream.
void writeBoolean(boolean v) It is used to write Boolean to the output stream as a 1-byte value.
void writeChar(int v) It is used to write char to the output stream as a 2-byte value.
void writeChars(String s) It is used to writestringto the output stream as a sequence of characters.
void writeByte(int v) It is used to write a byte to the output stream as a 1-byte value.
void writeBytes(String s) It is used to write string to the output stream as a sequence of bytes.
void writeInt(int v) It is used to write an int to the output stream
void writeShort(int v) It is used to write a short to the output stream.
void writeShort(int v) It is used to write a short to the output stream.
void writeLong(long v) It is used to write a long to the output stream.
void writeUTF(String str) It is used to write a string to the output stream using UTF-8 encoding in portable manner.
void flush() It is used to flushes the data output stream.

Example of DataOutputStream class

In this instance, we are demonstrating how to use the DataOutputStream class to write data to a text file named testout.txt.

Example

package com.example;



import java.io.*;

public class OutputExample {

	public static void main(String[] args) throws IOException {

		FileOutputStream file = new FileOutputStream(D:\\testout.txt);

		DataOutputStream data = new DataOutputStream(file);

		data.writeInt(65);

		data.flush();

		data.close();

		System.out.println("Succcess...");

	}

}

Output:

Output

Succcess...

testout.txt:

Input Required

This code uses input(). Please provide values below: