Java.io.DataInputStream class in Java | Set 1

A data input stream enable an application 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.

Constructor:

  • DataInputStream(InputStream in) : Creates a DataInputStream that uses the specified underlying InputStream.

Important Methods:

  • int read(byte[] b) : Reads some number of bytes from the contained input stream and stores them into the buffer array b.
    Syntax: public final int read(byte[] b)
                   throws IOException
    Parameters:
    b - the buffer into which the data is read.
    Returns:
    the total number of bytes read into the buffer, 
    or -1 if there is no more data because the end of the stream has been reached.
    Throws:
    IOException
    
  • int read(byte[] b, int off, int len) : Reads up to len bytes of data from the contained input stream into an array of bytes.
    Syntax:public final int read(byte[] b,
           int off,
           int len)
                   throws IOException
    Parameters:
    b - the buffer into which the data is read.
    off - the start offset in the destination array b
    len - the maximum number of bytes read.
    Returns:the total number of bytes read into the buffer, or -1 if there is no more data 
    Throws:
    NullPointerException 
    IndexOutOfBoundsException 
    IOException 
    
  • boolean readBoolean() : Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
    Syntax:public final boolean readBoolean()
                              throws IOException
    Returns:the boolean value read.
    Throws:
    EOFException
    IOException 
    
  • char readChar() : Reads two input bytes and returns a char value.
    Syntax:public final char readChar()
                        throws IOException
    Returns:the next two bytes of this input stream, interpreted as a char.
    Throws:
    EOFException 
    IOException
  • double readDouble() : Reads eight input bytes and returns a double value.
    Syntax:public final double readDouble()
                            throws IOException
    Returns:
    the next eight bytes of this input stream, interpreted as a double.
    Throws:
    EOFException
    IOExceptioN
  • int readInt() : Reads four input bytes and returns an int value.
    Syntax:public final int readInt()
                      throws IOException
    Returns: the next four bytes of this input stream, interpreted as an int.
    Throws:
    EOFException
    IOException
    

The DataInputStream class is often used together with a DataOutputStream.

Implementations of some of the important methods
Program 1:

//Java program to demonstrate DataInputStream
// This program uses try-with-resources. It requires JDK 7 or later.
import java.io.*;
class DataInputStreamDemo 
{
	public static void main(String args[]) throws IOException 
	{
		//writing the data.
		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;
		}
		
		// reading the data back.
		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();
			System.out.println("Values: " + a + " " + b + " " + c+" " + d);
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Cannot Open the Input File");
			return;
		}
	}
}

Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Output:

Values: 1.1 55 true 4

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 contribute.geeksforgeeks.org or mail your article to contribute@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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.