Table of contents
1.
Introduction
2.
Java DataInputStream Class
3.
Java DataInputStream Class Methods
4.
Java DataOutputStream Class
5.
Java DataOutputStream Class Methods
6.
Example
7.
Code 1
8.
Code 2
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

Data Input/Output Stream

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this section, we will be covering Data Input/Output Stream, but before moving to the topic, let us brush up on a few things before. The one is what is an InputStream. An InputStream is used to read a stream of raw data bytes in image data, audio, video, etc. A character-stream data can also be used to read the streams of characters that can be used from FileReader class.

Also see,  Swap Function in Java

Java DataInputStream Class

This class works machine-independent, allowing an application to read primitive data from an input stream. These generally use the data output stream, which can be used after the input stream to read the data.

Declaration:

public class DataInputStream extends FilterInputStream implements DataInput

Java DataInputStream Class Methods


Java DataOutputStream Class

This class works on the principle of allowing an application to write primitive data types to the output stream in a machine-independent way. These can be read by the input stream written by the data output stream.

Declaration:

public class DataOutputStream extends FilterInputStream implements DataOutput

Java DataOutputStream Class Methods

Example

Contents of out.txt:

Line A
Line B
Line C
Line A
Line B
Line C

Code 1

import java.io.*;    
public class DataInputStream1 {


	public static void main(String[] args) throws IOException {  
		InputStream input = new FileInputStream("out.txt");  
		DataInputStream inst = new DataInputStream(input);  
		int count = input.available();  
		byte[] ary = new byte[count];  
		inst.read(ary);  
		for (byte bt : ary) {  
			char k = (char) bt;  
			System.out.print(k+"-");  
		}  
	}  
}  
You can also try this code with Online Java Compiler
Run Code

 

Output

L-i-n-e- -A-
-
-L-i-n-e- -B-
-
-L-i-n-e- -C-
-
-L-i-n-e- -A-
-
-L-i-n-e- -B-
-
-L-i-n-e- -C-
-
-
You can also try this code with Online Java Compiler
Run Code

In this example, we have used InputStream to get the new file and then parsed that file using the DataInputStream. We also have used some in-built functions of the DataInputStream. 

Try it on online java compiler.

Code 2

import java.io.*;
public class DataOutputStream1 {


	public static void main(String[] args) throws IOException {  
		FileOutputStream file = new FileOutputStream("out.txt");  
		DataOutputStream data = new DataOutputStream(file);  
		data.writeInt(65);  
		data.flush();  
		data.close();  
		System.out.println("Succcess...");  
	}  
}  
You can also try this code with Online Java Compiler
Run Code

Output

Succcess…
You can also try this code with Online Java Compiler
Run Code

In this example, we have used OutputStream to get the new file and then parsed that file using the DataOutputStream. We also have used some in-built functions of the DataOutputStream. 

Also see, Duck Number in Java and Hashcode Method in Java

FAQs

  1. Why it is called a data input stream?
    It is called DataInputStram because it reads the data bytes. An application that uses a data output stream to write data utilised later for further processing.
     
  2. What is dataoutputstream in Java?
    It is a class that allows an application to write primitive Java data types to the output stream. 
     
  3. How to write primitive Java data types to the output stream?
    This class allows an application to write primitive Java data types. It generally utilizes the data output stream to write data which an input stream can later use.

Key Takeaways

In this article, we have extensively discussed about Java DataInput/Output stream in java. We briefly introduced these two topics along with their declarations. We also covered different types of methods and their description, which might be helpful during the execution. We also took a few examples, thereby explaining different methods.

We hope that this blog has helped you enhance your knowledge regarding Java DataInput/Output stream in java and if you would like to learn more, check out our articles on java here

You can learn about the file Input stream class by clicking File Input Stream Class.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass