Java DataInputStream Class

To read raw data bytes from images, audio, video, etc., utilise an input stream. The FileReader class allows for reading character streams, which can also be read using character-stream data.

Explanation

An application can read raw data from the input stream in a machine-independent manner thanks to the Java DataInputStream class.

Typically, a Java application writes data to the data output stream so that a data input stream can read it later.

Syntax

InputStream in =newDataInputStream(InputStream in);

Constructor

DataInputStream(InputStream in) uses the underlying InputStream to create a DataInputStream.

Method

Method Description
read(byte[] b) Reads a predetermined number of bytes from the contained input stream and stores them in buffer array b.
read(byte[] b, int off, int len) Reads data from the contained input stream up to length bytes and stores it in an array.
readBoolean() Reads a single input byte, returns false if the byte is zero, and true otherwise.
readChar() Returns a char value after reading two input bytes.
readByte() Returns a byte value after reading one input byte.
readFloat() Returns a float value after reading four input bytes.
readFully() Read the number of bytes in the byte array.
readDouble() Returns a double value after reading eight bytes of input.
readInt() Returns an int value after reading four input bytes.
readLine() Reading a text line
readLong() Returning a long value after reading eight input bytes
readShort() Return a short value after reading two input bytes.
readUnsignedByte() Read a byte, then give back an integer.
readUnsignedShort() After reading two input bytes, an integer array is returned.
skipBytes() Skips n bytes of the input stream’s data.

 

Sno     Method    Description 
1 public final int read(byte[] r, int off, int len)throws IOException Reads data from the input stream up to len bytes into an array of bytes. gives back the total number of bytes that were read into the buffer; if the file is at the end, it returns -1.
2 Public final int read(byte [] b)throws IOException Reads a small number of bytes from the inputstream and puts them in the byte array. returns -1 if the file is at the end, otherwise returns the total number of bytes read into the buffer.
3
  • public final Boolean readBooolean()throws IOException
  • public final byte readByte()throws IOException
  •  public final short readShort()throws IOException
  • public final Int readInt()throws IOException
These methods will read the bytes from the contained InputStream. gives back the next two InputStream bytes as the designated primitive type.
4 public String readLine() throws IOException Opens the input stream and begins reading the next line of text. The characters read are returned as a String after it reads consecutive bytes, translating each byte into a character one at a time until it comes across a line terminator or file end.

Example

import java.io.*;
public class TechVidvan {
   public static void main(String args[])throws IOException 
{

      DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("E:\\file.txt"));
      dataOut.writeUTF("welcome to TechVidvan tutorials");

      DataInputStream dataIn = new DataInputStream(new FileInputStream("E:\\file.txt"));

      while(dataIn.available()>0) {
         String k = dataIn.readUTF();
         System.out.print(k+" ");
      }
   }
}

Output
welcome to TechVidvan tutorials

Example

import java.io.*;    
public class TechVidvan
{  
  public static void main(String[] args) throws IOException 
{  
    InputStream input = new FileInputStream("D:\\testout.txt");  
    DataInputStream inst = new DataInputStream(input);  
    int count = input.available();  
    byte[] ary = new byte[count];  
    inst.read(ary);  
    for (byte bt : ary) 
{  
      char k = (char) bt;  
      System.out.print(k+"-");  
    }  
  }  
  }

Output
J-A-V-A

Example

import java.io.FileInputStream;
import java.io.InputStream;
class Main
 {
  public static void main(String args[])
 {

    byte[] array = new byte[100];

    Try
 {
      InputStream input = new FileInputStream("input.txt");

      System.out.println("Bytes in file: " + input.available());
      input.read(array);
      System.out.println("Data read : ");


      String data = new String(array);
      System.out.println(data);

      input.close();
    } 
catch (Exception e)
 {
      e.getStackTrace();
    }
  }
}

Output
Bytes in file: 16
Data read:
Java programming

Conclusion

In Java, DataInputStream is essentially used to read data from the input stream passed into the constructor as a file as an argument. It is capable of reading all Java primitive data types.