Java I/O Data Streams

In this tutorial we will discuss the Java I/O Data Streams.

Java I/O Data Streams

In this tutorial we will discuss the Java I/O Data Streams.

Java I/O Data Streams

Java I/O Data Streams

In this tutorial we will discuss the Java I/O Data Streams.

To deal with the binary I/O of primitive data type values as well as the String values Java provided the support of Data stream. DataInput or the DataOutput is the interface which is implemented by all the data streams classes.

DataInput : An interface that facilitate to read byte from the binary stream and rebuild that data into any of the Java primitive types along with this facility a String from data in the UTF-8 format can also be rebuild.

There are several classes that implements the DataInput interface. Some of them are as follows :

Classes Description
DataInputStream DataInputStream allows your application to read the Java primitive data type values from the stream. 
RandomAccessFile This class provides the facility to read and write to a random access file.
ObjectInputStream ObjectInputStream retrieves the previously serialized objects.

Methods of DataInput

  • readBoolean() : This method is used to read one input byte from the input stream and return 'false' incase of byte value is 'zero'.

    boolean readBoolean() throws IOException
     
  • readByte() : This method is used to read one input byte from the input stream and it returns that byte as a signed value range from -128 to 127.

    byte readByte() throws IOException
     
  • readChar() : This method is used to read two input bytes from the input stream and returns a char value.

    char readChar() throws IOException
     
  • readDouble() : This method is used to read eight (8) input bytes from the input stream and returns a double value

    double readDouble() throws IOException
     
  • readFloat() : This method is used to read four input bytes from the input stream and it returns float value.

    float readFloat() throws IOException
     
  • readFully(byte[] a) : This method is used to read the specified number of bytes from the input stream and kept them into the buffer array b (say).

    void readFully(byte[] b) throws IOException
     
  • readFully(byte[] a, int off, int len) : This method is used to read the number of bytes as specified into its parameter 'len'.

    void readFully(byte[] b, int off, int len) throws IOException
     
  • readInt() : This method is used to read four input bytes from the input stream and returns int value.

    int readInt() throws IOException
     
  • readLine() : This method is used to read the next line from the input stream.

    String readLine() throws IOException
     
  • readLong() : This method is used to read eight input bytes from the input stream and returns long value.

    long readLong() throws IOException

     
  • readShort() : This method is used to read two input bytes from the input stream and returns short value.

    short readShort() throws IOException
     
  • readUnsignedByte() : This method is used to read one input byte from the input stream and returns the unsigned 8-bit int value in the range 0-255.

     int readUnsignedByte() throws IOException
  • readUnsignedShort() : This method is used to read two input bytes from the input stream and returns the unsigned 16-bit int value in the range 0-65535

    int readUnsignedShort() throws IOException
     
  • readUTF() : This method is used to read the Unicode character string then encoded it as modified UTF-8 format and returns this string.

    String readUTF() throws IOException
     
  • skipBytes(int n) : This method is used to skip over the specified bytes of data and returns how many bytes are actually skipped.

    int skipBytes(int n) throws IOException
     

DataOutput : An interface that facilitate to convert the primitive types values of Java to a series ob bytes write byte to the binary stream.

There are several classes which implements the DataOutput interface. Some of them are as follows :

Classes Description
DataOutputStream DataOutputStream allows your application to write the Java primitive data type values to the output stream. 
RandomAccessFile This class provides the facility to read and write to a random access file.
ObjectOutputStream This class is used to write the primitive data types and graphs of Java objects into an output stream.

Methods of DataOutput

  • write(byte[] b) : This method is used to write all the bytes of a specified array (say, b is an array) into the output stream.

    void write(byte[] b) throws IOException

  • write(byte[] b, int off, int len) : This method is used to write the specified number of bytes from the specified array into the output stream.

    void write(byte[] b, int off, int len) throws IOException

  • write(int b) : This method is used to write for the integer argument to the output stream.

    void write(int b) throws IOException

  • writeBoolean(boolean v) :

    void writeBoolean(boolean v) throws IOException

  • writeByte(int v) : This method is used to write for the integer argument to the output stream but the written byte using this method can be read by readByte() method of DataInput.
    void writeByte(int v) throws IOException

  • writeBytes(String s) : This method is used to write string, in order to one byte, into the output stream.

    void writeBytes(String s) throws IOException

  • writeChar(int v) : This method is used to write the char value that made up from the two bytes, into the output stream.

    void writeChar(int v) throws IOException

  • writeChars(String s) : This method is used to write each characters of a specified string in order to two bytes per character, into the output stream.

    void writeChars(String s) throws IOException

  • writeDouble(double v) : This method is used to write the double value that made up from the eight bytes into the output stream.

    void writeDouble(double v) throws IOException

  • writeFloat(float v) : This method is used to write the float value that made up from the four bytes into the output stream.

    void writeFloat(float v) throws IOException

  • writeInt(int v) : This method is used to write the int value that made up from the four bytes into the output stream.

    void writeInt(int v) throws IOException

  • writeLong(long v) : This method is used to write the long value that made up from the eight bytes into the output stream.

    void writeLong(long v) throws IOException

  • writeShort(int v) : This method is used to write the two bytes into the output stream for displaying the argument's value

    void writeShort(int v) throws IOException

  • writeUTF(String s) : This method is used to write the string s into the output stream.

    void writeUTF(String s) throws IOException