Kayla Blain
Kayla Blain

Reputation: 13

Using ObjectWriter. Gives me EOF Exception even though I close the file.

/*This program serializes the students. *If the file exists, then the program loads it. *Otherwise it makes a new class list. *Student objects are added to the class list then saved */ It is part of an assignment, however I've never used objectWriter before.

import java.util.Scanner; 
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.ClassNotFoundException;
import java.util.List;
import java.util.ArrayList;
import java.io.EOFException;

public class ClassList{

 public static void find(int sN, String fN, String lN){
    boolean found = false;

    try{
    ObjectOutputStream objectOS = new ObjectOutputStream(new FileOutputStream("ClassList.data"));
        ObjectInputStream objectIS = new ObjectInputStream(new FileInputStream("ClassList.data"));
        int i=0;
        Assignment readAssign = (Assignment) objectIS.readObject();
    Student studentRead = readAssign.getStudents().get(i);

        /*Search the student objects & compare student number.
         *If found, boolean returns true, else the boolean is false
         */ 
        while (readAssign.getStudents() != null){
             studentRead = readAssign.getStudents().get(i);
             if (studentRead.getID() == sN){
                  found = true;
                  break;
             }
             i++;
        }

        /*If the boolean found is true error message is produced
         *If the boolean found is false then it adds a new Student object
         */
        if (found = true){
             System.out.println("A student with this id number already exists by the name:");
             System.out.print(studentRead.getFName() + " " + studentRead.getLName());
         objectOS.flush();
     objectOS.close();
        }
        else{
             Student student = new Student();
             student.setFName(fN);
             student.setLName(lN);
             student.setID(sN);
             Assignment assign = new Assignment();
     List<Student> students = new ArrayList<>();
     students.add(student);
     assign.setStudents(students);
             System.out.println("Done");
             objectOS.writeObject(assign);
         objectOS.flush();
     objectOS.close();


        }

    }
    catch(ClassNotFoundException cnf){
     cnf.printStackTrace();
}
    catch(FileNotFoundException fnf){
     fnf.printStackTrace();
}
catch(EOFException of) {
     eof.printStackTrace();
}
    catch(IOException ioe) {
     //ioe.printStackTrace();
}

 }



  public static void main(String args[]){
        Scanner stuIn = new Scanner(System.in);

        //Use student Input to create students
        System.out.println("What is your first name: ");
        String fName = stuIn.nextLine();

        System.out.println("What is your last name: ");
        String lName = stuIn.nextLine();

        System.out.println("What is your student number: ");
        int sNum = stuIn.nextInt();

        find(sNum, fName, lName);

 }


}

Error Message:

java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2601) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at ClassList.find(ClassList.java:28) at ClassList.main(ClassList.java:100)

Upvotes: 1

Views: 149

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503090

This statement:

ObjectOutputStream objectOS = new ObjectOutputStream(new 
    FileOutputStream("ClassList.data"));

... overwrites the existing file with an empty file. You then try to read an object from that empty file.

You should read first, then open the file for output.

I'd actually break this code into separate methods:

  • Read assignment (open, read, close)
  • Process assignment (no IO)
  • Write assignment (open, writem, close)

Also note that:

if (found = true)

doesn't do what you want it to. It will assign to found, and then always execute the body of the if block. You want:

if (found == true)

or better yet

if (found)

Upvotes: 1

Related Questions