Signup/Sign In
PUBLISHED ON: FEBRUARY 25, 2021

Java DataOutputStream Class

In this tutorial, we will learn about DataOutputStream class in Java. This class is used to write primitive data into files. This class is very helpful because later we extract this data by using DataInputStream class. Here primitive data type includes short, char, int, float, double and boolean. DataOutputStream class also know as a filter class which wraps output stream into primitive data.

Syntax

This is the syntax declaration for DataOutputStream.

public class DataOutputStream extends FilterOutputStream implements DataOutput 

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 write string to 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 with writeInt()Method

In the following program, we are implementing the writeInt() method to write data into the file.

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		FileOutputStream file = new FileOutputStream("E:\\studytonight\\myfile.txt");  
        DataOutputStream data = new DataOutputStream(file);  
        data.writeInt(68);  
        data.flush();  
        data.close();  
        System.out.println("Data is written to the file successfully"); 
	}  
}


Data is written to the file successfully

myfile.txt

 A

Example of DataOutputStream class Methods

In the following program, we are using various methods like writeDouble(), writeInt(), writeBoolean() and writeChar(). These methods will store the given data in a primitive data type. Now we can see this data is not in a readable format but when we access it by using DataInputStream class it is much more convenient to read input data as it is.

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		FileOutputStream file = new FileOutputStream("E:\\studytonight\\myfile.txt");  
        DataOutputStream data = new DataOutputStream(file);  
        data.writeDouble(2.1); 
        data.writeInt(45); 
        data.writeBoolean(true); 
        data.writeChar('S'); 
        data.flush();  
        data.close();  
        System.out.println("Data is written to the file successfully"); 
	}  
}


Data is written to the file successfully

myfile.txt

@ ÌÌÌÌÌÍ   - S

Conclusion

In this tutorial, we learned about DataOutputStream class in Java. This class is used to store data to file in primitive data types so later can be retrieved as same.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.