Program 24: Binary Input
Code:
import [Link].*;
public class ReadStudentBinaryFile {
public static void main() {
try {
FileInputStream fileIn = new FileInputStream("[Link]");
DataInputStream dataIn = new DataInputStream(fileIn);
String name = [Link]();
int rollNo = [Link]();
double marks = [Link]();
[Link]("Student Details:");
[Link]("Name: " + name);
[Link]("Roll Number: " + rollNo);
[Link]("Marks: " + marks);
[Link]();
[Link]();
} catch (IOException e) {
[Link]("An error occurred while reading the file.");
[Link]();
}
}
Algorithm
Step 1: Import the required package [Link].* for file handling.
Step 2: Declare the class ReadStudentBinaryFile.
Step 3: Declare the main function.
Step 4: Use a try block to handle potential exceptions.
Step 4.1: Create a FileInputStream object to open the file "[Link]".
Step 4.2: Wrap the FileInputStream object with a DataInputStream to read binary data.
Step 5: Read the student details from the file in binary format.
Step 5.1: Use readUTF() to read the student's name as a string.
Step 5.2: Use readInt() to read the roll number as an integer.
Step 5.3: Use readDouble() to read the marks as a double.
Step 6: Display the student details.
Step 6.1: Print the name in the format Name: <name>.
Step 6.2: Print the roll number in the format Roll Number: <roll number>.
Step 6.3: Print the marks in the format Marks: <marks>.
Step 7: Close the DataInputStream and FileInputStream to release file resources.
Step 8: Use a catch block to handle IOException.
Step 8.1: If an error occurs, display "An error occurred while reading the file.".
Step 9: End the program.