Java.io.DataInputStream Class



Introduction

The Java.io.DataInputStream class lets an application read primitive Java data types from an underlying input stream in a machine-independent way.Following are the important points about DataInputStream −

  • An application uses a data output stream to write data that can later be read by a data input stream.

  • DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.

Class declaration

Following is the declaration for Java.io.DataInputStream class −

public class DataInputStream
   extends FilterInputStream
      implements DataInput

Field

Following are the fields for Java.io.DataInputStream class −

  • protected InputStream in − This is the input stream to be filtered.

Class constructors

Sr.No.Constructor & Description
1

DataInputStream(InputStream in)

This creates a DataInputStream that uses the specified underlying InputStream.

Class methods

Sr.No.Method & Description
1int read(byte[] b)

This method reads some number of bytes from the contained input stream and stores them into the buffer array b

2int read(byte[] b, int off, int len)

This method reads up to len bytes of data from the contained input stream into an array of bytes.

3boolean readBoolean()

This method reads one input byte and returns true if that byte is nonzero, false if that byte is zero.

4byte readByte()

This method reads and returns one input byte.

5char readChar()

This method reads two input bytes and returns a char value.

6double readDouble()

This method reads eight input bytes and returns a double value.

7float readFloat()

This method reads four input bytes and returns a float value.

8void readFully(byte[] b)

This method reads some bytes from an input stream and stores them into the buffer array b.

9void readFully(byte[] b, int off, int len)

This method reads len bytes from an input stream.

10int readInt()

This method reads four input bytes and returns an int value.

11long readLong()

This method reads eight input bytes and returns a long value.

12short readShort()

This method reads two input bytes and returns a short value.

13int readUnsignedByte()

This method reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255.

14int readUnsignedShort()

This method reads two input bytes and returns an int value in the range 0 through 65535.

15String readUTF()

This method reads in a string that has been encoded using a modified UTF-8 format.

16static String readUTF(DataInput in)

This method reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.

17int skipBytes(int n)

This method makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.FilterInputStream
  • Java.io.Object