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
-
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.
-
What is dataoutputstream in Java?
It is a class that allows an application to write primitive Java data types to the output stream.
-
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!