gpt4 book ai didi

java - 使用对象编写器。即使我关闭文件,也会给我 EOF 异常。

转载 作者:太空宇宙 更新时间:2023-11-04 06:23:25 25 4
gpt4 key购买 nike

/*该程序对学生进行序列化。 *如果文件存在,则程序加载它。 *否则它会创建一个新的类列表。 *学生对象被添加到类(class)列表中然后保存 */它是作业的一部分,但我以前从未使用过 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 在 java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2601) 在 java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319) 在 java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) 在 ClassList.find(ClassList.java:28) 在 ClassList.main(ClassList.java:100)

最佳答案

此声明:

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

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

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

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

  • 阅读作业(打开、阅读、关闭)
  • 进程分配(无 IO)
  • 写入作业(打开、写入、关闭)

另请注意:

if (found = true)

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

if (found == true)

或者更好

if (found)

关于java - 使用对象编写器。即使我关闭文件,也会给我 EOF 异常。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27149699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com