PUBLISHED ON: FEBRUARY 25, 2021
Java DataInputStream Class
In this tutorial, we will learn about DataInputStream
in Java. This class reads primitive data from the stream of data without depending on the machine. Java application generally uses the DataOutpuStream
to write data that can later be read by a DataInputStream
.
Syntax
This is the internal syntax declaration of DataInputStream
class.
public class DataInputStream extends FilterInputStream implements DataInput
Methods
Method |
Description |
int read(byte[] b) |
It is used to read the number of bytes from the input stream. |
int read(byte[] b, int off, int len) |
It is used to read len bytes of data from the input stream. |
int readInt() |
It is used to read input bytes and return an int value. |
byte readByte() |
It is used to read and return the one input byte. |
char readChar() |
It is used to read two input bytes and returns a char value. |
double readDouble() |
It is used to read eight input bytes and returns a double value. |
boolean readBoolean() |
It is used to read one input byte and return true if byte is non zero, false if byte is zero. |
int skipBytes(int x) |
It is used to skip over x bytes of data from the input stream. |
String readUTF() |
It is used to read a string that has been encoded using the UTF-8 format. |
void readFully(byte[] b) |
It is used to read bytes from the input stream and store them into the buffer array. |
void readFully(byte[] b, int off, int len) |
It is used to read len bytes from the input stream. |
Example of DataInputStream class to read data from file
In the program given below, we are reading data from the given file myfile.txt
using read()
method of DataInputStream
class. Firstly we will count the available bytes to read in a file using available()
method and create a new byte array of that size. Now we need to read data from a file using the read() method and then we are printing it one by one.
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
FileInputStream file = new FileInputStream("E:\\studytonight\\myfile.txt");
DataInputStream inst = new DataInputStream(file);
int count = file.available();
byte[] array = new byte[count];
inst.read(array);
for (byte b : array) {
char ch = (char) b;
System.out.print(ch);
}
}
}
Hello Studytonight
myfile.txt
Hello Studytonight
Example of reading and writing primitive data:
The following example is very important in terms of the actual use of DataInputStream
class and DataOutputStream
class both, here we will first write the data into a file and the read that data again. When we store the data it will not be in a human-readable form but we can read it properly in a primitive type using DataInputStream
.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("E:\\studytonight\\myfile.txt"));
dataOutputStream.writeInt(45);
dataOutputStream.writeFloat(98.7F);
dataOutputStream.writeLong(123456);
dataOutputStream.close();
DataInputStream dataInputStream =new DataInputStream(new FileInputStream("E:\\studytonight\\myfile.txt"));
int intData = dataInputStream.readInt();
float floatData = dataInputStream.readFloat();
long longData = dataInputStream.readLong();
dataInputStream.close();
System.out.println("int data: " + intData);
System.out.println("float data: " + floatData);
System.out.println("long data: " + longData);
}
}
int data: 45
float data: 98.7
long data: 123456
myfile.txt
-BÅff â@
Conclusion
In this tutorial, we learned about DataInputStream class and its methods in Java. This class reads primitive data from the stream of data without depending on the machine.