Java.io.DataInputStream class in Java | Set 1 - GeeksforGeeks

21 Sep, 2021
A data input stream enables an application to read primitive Java data types from an underlying input stream in a machine-independent way(instead of raw bytes). That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes. 
An application uses a data output stream to write data that can later be read by a data input stream. Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8. DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.
Firstmost let us do discuss the constructor of this class 
Now let us discuss methods of this class that are depicted below in a tabular format as shown below as follows: 
Remember: The DataInputStream class is often used together with a DataOutputStream.
Implementation:
Now let us do implement a few of the above methods of this class has been discussed above
Below program uses try-with-resources. It requires JDK 7 or later as concept of try-catch block was introduced in Java7
Example 1 
Java
// Java program to Demonstrate DataInputStream Class // Importing I/O classesimport java.io.*; // Main classclass DataInputStreamDemo {     // Main driver method    public static void main(String args[]) throws IOException {         // Writing the data         // Try block to check for exceptions        try ( DataOutputStream dout =                        new DataOutputStream(new FileOutputStream("file.dat")) ) {             dout.writeDouble(1.1);            dout.writeInt(55);            dout.writeBoolean(true);            dout.writeChar('4');        }         // Catch block to handle the exceptions        catch (FileNotFoundException ex) {             // Display message when FileNotFoundException occurs            System.out.println("Cannot Open the Output File");            return;        }         // Reading the data back.         // Try block to check for exceptions        try ( DataInputStream din =                        new DataInputStream(new FileInputStream("file.dat")) ) {             // Illustrating readDouble() method            double a = din.readDouble();             // Illustrating readInt() method            int b = din.readInt();             // Illustrating readBoolean() method            boolean c = din.readBoolean();             // Illustrating readChar() method            char d = din.readChar();             // Print the values            System.out.println("Values: " + a + " " + b + " " + c + " " + d);        }         // Catch block to handle the exceptions        catch (FileNotFoundException e) {             // Display message when FileNotFoundException occurs            System.out.println("Cannot Open the Input File");            return;        }    }}
Output:
Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.Next Article: Java.io.DataInputStream class in Java | Set 2This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
nireshvardhan
simranarora5sos
Java-I/O
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java