FileInputStream, DataInputStream in java
In the Java programming language, both FileInputStream and DataInputStream are file processing methods used to read binary data and information from existing files created in Java. In Java file processing, FileInputStream and DataInputStream complete individual file management tasks and, if needed, provide individual multiple file functions.

So, let’s learn in detail about both the FileInputStream and DataInputStream classes in Java.
FileInputStream class in Java.
FileInputStream is a built-in class function in the Java library that helps Java users read raw byte data from a file. FileInputStream in Java is a built-in part of the java.io library package. It is designed to read the contents of a file byte-by-byte using the FileInputStream function in the Java language.
Special features of FileInputStream in Java.
- File Reading – This function helps read raw byte data/binary data from a user-created file.
- Simple interface – This is a simple interface method, which contains methods for reading data bytes one by one or into a buffer.
- Byte-oriented – This is the correct choice for existing binary files in Java, such as images, audio, and video file formats, where user-defined characters may not necessarily be the important data to be read.
Common methods of FileInputStream in Java.
- read() – This function reads the next byte of data from the file. It returns the file data byte as an integer between 0 and 255 characters, or -1 for the end of the file.
- read(byte[] b) – This function reads data up to b.length bytes into a user-defined array in the file process.
- close() – This closes the input stream in the Java file handling process, and frees the existing system resources associated with it.
Example of FileInputStream in Java.
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamIllustration {
public static void main(String[] args) {
FileInputStream filinptstrm = null;
try {
filinptstrm = new FileInputStream(“sample.bin”); // Here we try to open a binary file with the FileInputStream method
int byteData;
while ((byteData = filinptstrm.read()) != -1) { // Here it reads the file byte by byte data
System.out.print((char) byteData); // Here it prints as characters, assuming it’s character file data
}
} catch (IOException e) {
System.out.println(“Here it displays an error while reading the file – ” + e.getMessage());
} finally {
try {
if (filinptstrm != null) {
filinptstrm.close(); // Here we close the stream after reading the file
}
} catch (IOException e) {
System.out.println(“Here Error closing the binary file ” + e.getMessage());
}
}
}
}
Special features of the Java FileInputStream function method.
- In this program, FileInputStream is used to read raw byte data from the sample.bin file.
- This function reads binary file data byte-by-byte, so this method is not suitable for reading data that needs to be understood or analysed in Java as characters or structured data, such as text files or numbers.
- For example, if a file contains character data, if the file contains binary data, such as an image, the file bytes will be printed to the screen as they are.
DataInputStream class in Java.
In the Java library, DataInputStream is a subclass of FilterInputStream, and the DataInputStream function method in Java is used to read data and information from primitive data types, such as int, float, double, long, char, and boolean, from an input stream in a machine-independent order. The DataInputStream function method class in Java can be used to read binary file data created by applying DataOutputStream.
Special features of DataInputStream in Java.
- Reading primitive data – It helps Java programs read primitive data types like int, float, char directly from a file.
- Machine-independent – Java provides the user with the DataInputStream function method to read binary file data in a platform-independent order.
- Buffered input – It is the opposite of FileInputStream in a Java program, but is not directly byte-oriented and can analyze or understand user-defined file data based on its type.
Basic methods in DataInputStream in Java.
- readInt() – Reads int-value data types from the input stream in the file handling process.
- readDouble() – Reads double-value data types from the file handling process.
- readChar() – Reads char-value data types from the file handling process.
- readUTF() – Reads string data information encoded in UTF-8 character format from the file handling process.
- close() – Closes the open stream in the file handling process and frees the file stream system resources.
Java DataInputStream usage example.
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamIllustration {
public static void main(String[] args) {
DataInputStream dtainptstrm = null;
try {
dtainptstrm = new DataInputStream(new FileInputStream(“testfile.bin”));
// here we Read different types of data from the file stream
int integer = dtainptstrm.readInt();
double info = dtainptstrm.readDouble();
String str_value = dtainptstrm.readUTF();
System.out.println(“Here is the integer data info – ” + integer);
System.out.println(“Here is the Double data info – ” + info);
System.out.println(“Here is the String data info – ” + str_value);
} catch (IOException e) {
System.out.println(“Display Error when reading the file – ” + e.getMessage());
} finally {
try {
if (dtainptstrm != null) {
dtainptstrm.close(); // Here it closes the stream after reading the file
}
} catch (IOException e) {
System.out.println(“Here it displays an error when closing the file – ” + e.getMessage());
}
}
}
}
}
Features of Java DataInputStream and DataOutputStream.
- The DataInputStream function method is used in Java file handling when a Java user needs to read binary file data, such as integers, floats, and variable parameter data type information, created using a DataOutputStream.
- It automatically manages or handles the task of converting or replacing byte data into the correct primitive data type in the Java file handling process.
- The DataInputStream in Java is designed to read structured binary file data, such as primitive data type information, from a stream.
