使用ObjectWriter。即使我关闭文件,也给我EOF异常。

时间:2014-11-26 12:50:11

标签: java

/ *此程序序列化学生。  *如果文件存在,则程序加载它。  *否则它会创建一个新的类列表。  *学生对象被添加到班级列表中然后保存  * / 它是作业的一部分,但我之前从未使用过objectWriter。

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);

 }


}

错误讯息:

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)     在ClassList.find(ClassList.java:28)     在ClassList.main(ClassList.java:100)

1 个答案:

答案 0 :(得分:1)

本声明:

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

...用空文件覆盖现有文件。然后尝试从该空文件中读取对象。

您应首先阅读然后打开文件进行输出。

我实际上将此代码分解为单独的方法:

  • 阅读作业(开放,阅读,关闭)
  • 流程分配(无IO)
  • 编写作业(开放,写作,关闭)

另请注意:

if (found = true)

没有做你想要的。它将分配found,然后始终执行if块的主体。你想要:

if (found == true)

或更好

if (found)