DataInputStream in Java



DataInputStream File Java

DataInputStream in Java

Java DataInputStream is a class that we use to read primitive data type values. We can use this stream along with other input streams like FileInputStream to read the data. Since it reads data as numbers instead of bytes, we call it as DataInputStream. It contains only a single constructor that accepts an InputStream as a parameter.

DataInputStream di = new DataInputStream(InputStream in);

Methods of Java DataInputStream

Below are the methods that the DataInputStream class supports for write and other operations.

MethodsDescription
int available()Returns the estimated number of remaining bytes available to read
void close()Closes the input stream
void mark(int readLimit)Marks the current position in the input stream
boolean markSupported()Checks if the input stream supports mark() and reset() methods
int read()Reads the next byte of data in the input stream
int read(byte[] b)Reads an array of bytes of data from the input stream
int read(byte[] b, int off, int len)Reads len bytes of data from the input stream starting from the offset position
boolean readBoolean()Reads the boolean value from the stream
byte readByte()Reads the next byte of data from the input stream
char readChar()Reads the next char of data from the input stream
double readDouble()Reads the next double data from the input stream
float readFloat()Reads the next float data from the input stream
void readFully(byte[] b)Reads all bytes of data from the input stream and stores in the buffer array b
void readFully(byte[] b, int off, int len)Reads len bytes of data from the input stream starting from the offset into the byte buffer array
int readInt()Reads the next integer data from the input stream
String readLine()Reads a line of string from the input stream
long readLong()Reads a long data from the input stream
byte[] readNBytes(int len)Reads the specific length of bytes from the input stream and stores into the buffer
int readNBytes(byte[] b, int off, int len)Reads len number of bytes from the input stream from the offset position and stores in buffer array b
short readShort()Reads a short data from the input stream
int readUnsignedByte()Reads the next unsigned bytes of data from the input stream
int readUnsignedShort()Reads the next unsigned short data from the input stream
String readUFT()Reads the unicode form of data from the input stream
long skip(long n)Skips and discards n number of bytes to read from the input stream
int skipBytes(int n)Skips and discards the specific number of bytes during the read operation

Example

The below example uses the read() method of the DataInputStream class to read the data from the input file. We can check the available number of bytes to read using the available() method.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.DataInputStream;

public class DataInputStreamDemo {

  public static void main(String[] args) throws IOException {
    FileInputStream fi = new FileInputStream("InputFile.txt");
    DataInputStream di = new DataInputStream(fi);
    
    System.out.println("Available number of bytes to read: " + di.available());
    int a;
    while((a=di.read()) != -1)
      System.out.print((char)a);

  }

}
Available number of bytes to read: 37
This is an example of DataInputStream

Example: DatatInputStream different read methods

The below example demonstrates the various read methods to read data from the file using the DataInputStream class.

The readUFT() method is for reading the Unicode format data, readInt() method for  integer value, readChar() for character data, readBoolean() for boolean value, readDouble() for double data, readFloat() for float value and readLong() for long value.

There are corresponding write methods in the DataOutputStream class to write these values which we will see in detail in the next tutorial.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class DataInputStreamDemo {

  public static void main(String[] args) throws IOException {
    FileInputStream fi = new FileInputStream("OutputFile.txt");
    DataInputStream di = new DataInputStream(fi);
    
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    DataOutputStream dos = new DataOutputStream(fo);
    
    dos.writeUTF("DataInputStream");
    dos.writeInt(25);
    dos.writeChar('d');
    dos.writeBoolean(false);
    dos.writeDouble(233.34);
    dos.writeFloat(555.55f);
    dos.writeLong(8888888);
    
    
    System.out.println("Available number of bytes to read: " + di.available());

    System.out.println("Read UFT: " + di.readUTF());
    System.out.println("Read int: " + di.readInt());
    System.out.println("Read char: " + di.readChar());
    System.out.println("Read boolean: " + di.readBoolean());
    System.out.println("Read double: " + di.readDouble());
    System.out.println("Read float: " + di.readFloat());
    System.out.println("Read Long: " + di.readLong());
    
    
    di.close();
    fi.close();
    dos.close();
    fo.close();
  }

}
Available number of bytes to read: 44
Read UFT: DataInputStream
Read int: 25
Read char: d
Read boolean: false
Read double: 233.34
Read float: 555.55
Read Long: 8888888

Example: Read the specific length of data using DataInputStream

We can also read the specific length of data using the DataInputStream class in Java. The below example shows how to read the text of length 8 from the input file. We are allocating only 12 bytes and hence the remaining 4 bytes are filled with *.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.DataInputStream;

public class DataInputStreamDemo {

  public static void main(String[] args) throws IOException {
    FileInputStream fi = new FileInputStream("InputFile.txt");
    DataInputStream di = new DataInputStream(fi);
    byte[] b = new byte[12];
    int a = di.read(b, 0, 8);
    
    for(byte by : b) {
      char ch = (char)by;
      if(by == 0)
        ch = '*';
      System.out.print(ch);
    }
    
    di.close();
    fi.close();

  }

}
This is ****

Example: DataInputStream skip() method

The below example demonstrates how to use the skip() method that skips or discards specific characters during the read operation. Here, we are discarding the first 4 characters and hence reads only from the 5th character.

import java.io.*;

public class SkipDISDemo {

  public static void main(String[] args) throws IOException {
    FileInputStream fi;
    try {
      fi = new FileInputStream("InputFile.txt");
      DataInputStream di = new DataInputStream(fi);
      
      di.skip(4);
      int a = 0;
      while((a=di.read()) != -1)
        System.out.print((char)a);
      
      di.close();
      fi.close();
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    

  }

}
is an example of DataInputStream

 

Reference