0

I have a class that reads the written output of a Student[] array object

here is the code

import java.io.*;

// I use this class to read the final ouput "students_updated.dat"
public class StudentArrayObjectFileReader {

public static void main(String[] args) {
    try {
        ObjectInputStream fileReader = new ObjectInputStream(new FileInputStream("students_updated.dat"));
        Student[] studs = (Student[]) fileReader.readObject();
        fileReader.close();

        // List the records in console
        for (int i = 0; i < studs.length; i++) {
            System.out.printf("%7d %-35s %-5s %1d %-6s%n",studs[i].getID(), studs[i].getName(), studs[i].getCourse(), studs[i].getYr(), studs[i].getGender());
        }
    } catch (FileNotFoundException fnfe) {

        fnfe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }
}

problem is that it has an error when reading the Student[] studs = (Student[]) fileReader.readObject();

as stated here

java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at StudentArrayObjectFileReader.main(StudentArrayObjectFileReader.java:9)

any idea on what could be the problem? Thanks in advance..

this is how the students_updated.dat was written

    public void saveStudentArray() { // studs print to student_updated.dat
    try{
        output.writeObject(studs); // write the final studs
        output.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

with its ObjectInputStream declared in the constructor of where this method is

2 Answers 2

1

Check the contents of the students_updated.dat file. readObject() function expects the file to contain a serialized object.

Does the file contain a serialized object? Check if the serialization succeeded without anything going wrong?

If you're trying to construct the array from a plain text file, you should not use readObject().

Visit this link for a sample of how to serialize and deserialize an array - Can I serialize an array directly...

Sign up to request clarification or add additional context in comments.

1 Comment

It was the fact that the object array was not serialized. Thanks!
1

Move this line:

fileReader.close();

after your for loop.

In Java, the creation of a variable is simply creating a reference to some location in memory. The act of casting the object read from fileReader into your array of students just creates a pointer to the correct place in memory. If you then remove that place by closing the fileReader, you remove the location studs is pointing to.

4 Comments

it still returns the same exception. I also tried it without the fileReader.close() but not a chance of it working.
Are you sure your file has something in it? How is this data getting encoded?
yes there is a student[] in it, i used a system.out.print(); to make appear in the console and it does have contents
that is before writing the object by the way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.