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

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 

ConstructorAction PerformedDataInputStream(InputStream in)Creates a DataInputStream that uses the specified underlying InputStream.

Now let us discuss methods of this class that are depicted below in a tabular format as shown below as follows: 

Methods Action performed read(byte[] b)Reads some number of bytes from the contained input stream and stores them into the buffer array b.read(byte[] b, int off, int len)Reads up to length bytes of data from the contained input stream into an array of bytes. readBoolean()Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. readChar()Reads two input bytes and returns a char value. readUTF()Reads data from the underlying input stream and converts the bytes into a Unicode string.readByte()Read one input byte and returns a byte value.readFloat()Read four input bytes and returns a float value.readFully()Read bytes equal to the length of the byte arrayreadDouble()Reads eight input bytes and returns a double value.readInt()Reads four input bytes and returns an int value. readLine()Reading lines of textreadLong()Reading eight input bytes and returns a long valuereadShort()Read two input bytes and return a short value.readUnsignedByte()Read byte and return as an integerreadUnsignedShort()Read two input bytes and returns as an integer arrayskipBytes()Skips over n bytes of data from input stream

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




 

import java.io.*;

 

class DataInputStreamDemo {

 

    

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

 

        

 

        

        try ( DataOutputStream dout =

                        new DataOutputStream(new FileOutputStream("file.dat")) ) {

 

            dout.writeDouble(1.1);

            dout.writeInt(55);

            dout.writeBoolean(true);

            dout.writeChar('4');

        }

 

        

        catch (FileNotFoundException ex) {

 

            

            System.out.println("Cannot Open the Output File");

            return;

        }

 

        

 

        

        try ( DataInputStream din =

                        new DataInputStream(new FileInputStream("file.dat")) ) {

 

            

            double a = din.readDouble();

 

            

            int b = din.readInt();

 

            

            boolean c = din.readBoolean();

 

            

            char d = din.readChar();

 

            

            System.out.println("Values: " + a + " " + b + " " + c + " " + d);

        }

 

        

        catch (FileNotFoundException e) {

 

            

            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 2
This 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 [email protected]. 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.

My Personal Notes

arrow_drop_up