0% found this document useful (0 votes)
3 views3 pages

Program 7 Binary Input

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Program 7 Binary Input

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 24: Binary Input

Code:

import java.io.*;

public class ReadStudentBinaryFile {

public static void main() {

try {

FileInputStream fileIn = new FileInputStream("Student.dat");

DataInputStream dataIn = new DataInputStream(fileIn);

String name = dataIn.readUTF();

int rollNo = dataIn.readInt();

double marks = dataIn.readDouble();

System.out.println("Student Details:");

System.out.println("Name: " + name);

System.out.println("Roll Number: " + rollNo);

System.out.println("Marks: " + marks);

dataIn.close();

fileIn.close();

} catch (IOException e) {

System.out.println("An error occurred while reading the file.");

e.printStackTrace();

}
}
Algorithm
Step 1: Import the required package java.io.* 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 "Student.dat".

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.

You might also like