Java 如何将文件中的对象添加到ArrayList中?获得EOFEException
我正在做一个学校项目,它基本上允许用户创建、编辑和显示学生。我有一个Java 如何将文件中的对象添加到ArrayList中?获得EOFEException,java,exception,arraylist,java.util.scanner,eofexception,Java,Exception,Arraylist,Java.util.scanner,Eofexception,我正在做一个学校项目,它基本上允许用户创建、编辑和显示学生。我有一个createStudent(),它使用Scanner和ObjectOutputStream将信息写入文件。displayStudent()使用ObjectInputStream从文件中读取数据并显示。editStudent()的想法是要求用户输入他们想要编辑的学生的ID,然后更改日期并将其写回文件,我一直试图做的是使用ObjectInputStream从文件中读取数据,然后将该数据分配到ArrayList或HashMap,我想我
createStudent()
,它使用Scanner
和ObjectOutputStream
将信息写入文件。displayStudent()
使用ObjectInputStream
从文件中读取数据并显示。editStudent()
的想法是要求用户输入他们想要编辑的学生的ID,然后更改日期并将其写回文件,我一直试图做的是使用ObjectInputStream
从文件中读取数据,然后将该数据分配到ArrayList
或HashMap
,我想我会使用ArrayList,因为HashMap是无序的。当我尝试将文件中的数据添加到ArrayList时,出现以下错误:
java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3231)java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1663)java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:519)java.base/java.io.ObjectInputStream.readObject上的java.io.EOFEException(ObjectInputStream.java:477)在MidTermProject.editStudent(MidTermProject.java:194)在MidTermProject.main(MidTermProject.java:381)
这是我的editstudior()
代码:
public static void editStudent()引发IOException{
int editID;
弦乐学生;
ArrayList studentEdit=新建ArrayList();
扫描仪键盘=新扫描仪(System.in);
FileInputStream fstream=新的FileInputStream(“studentInfo.dat”);
ObjectInputStream inputFile=新ObjectInputStream(fstream);
System.out.print(“输入要编辑的学生的ID:”;
editID=keyboard.nextInt();
试一试{
student=(字符串)inputFile.readObject();
studentEdit.add(学生);
系统输出打印(studentEdit);
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
///添加了创建学生方法
公共静态void createStudent()引发IOException{
File File=新文件(“studentInfo.dat”);
boolean append=file.exists();
扫描仪键盘=新扫描仪(System.in);
试一试(
FileOutputStream fout=新的FileOutputStream(文件,追加);
中期项目OOT=新中期项目(fout,追加);
) {
id=idGenerator.getAndIncrement();
字符串convertedId=Integer.toString(getId());
oout.writeObject(convertedId);
System.out.print(“\n请在下面输入您的信息。\n”+“\n全名:”;
FullName=keyboard.nextLine();
oout.writeObject(全名);
系统输出打印(“地址:”);
地址=键盘.nextLine();
oout.writeObject(地址);
系统输出打印(“城市:”);
city=keyboard.nextLine();
oout.writeObject(城市);
系统输出打印(“状态:”);
state=keyboard.nextLine();
oout.writeObject(状态);
oout.close();
System.out.println(“完成!\n”);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
以下是中期项目的类代码
public class MidTermProject extends ObjectOutputStream {
private boolean append;
private boolean initialized;
private DataOutputStream dout;
static AtomicInteger idGenerator = new AtomicInteger(0001);
static int id;
public static String FullName;
public static String address;
public static String city;
public static String state;
public static String className;
public static String instructor;
public static String department;
public static String classNumber;
public static String courseNumber;
public static String year;
public static String semester;
public static String grade;
public static String studentID;
public static String courseID;
public static String enrollmentID;
public static HashMap<String, Integer> map = new HashMap<>();
Scanner keyboard = new Scanner(System.in);
protected MidTermProject(boolean append) throws IOException, SecurityException {
super();
this.append = append;
this.initialized = true;
}
public MidTermProject(OutputStream out, boolean append) throws IOException {
super(out);
this.append = append;
this.initialized = true;
this.dout = new DataOutputStream(out);
this.writeStreamHeader();
}
@Override
protected void writeStreamHeader() throws IOException {
if (!this.initialized || this.append) return;
if (dout != null) {
dout.writeShort(STREAM_MAGIC);
dout.writeShort(STREAM_VERSION);
}
}
公共类中期项目扩展了ObjectOutputStream{
私有布尔追加;
私有布尔初始化;
私有数据输出流dout;
静态AtomicInteger idGenerator=新的AtomicInteger(0001);
静态int-id;
公共静态字符串全名;
公共静态字符串地址;
公共静态字符串城市;
公共静态字符串状态;
公共静态字符串类名称;
公共静态弦指导员;
公共部门;
公共静态字符串类号;
公共静态字符串枚举器;
公共静态字符串年;
公共静态字符串;
公共静态串级;
公共静态字符串studentID;
公共静态字符串;
公共静态字符串enrollmentID;
public static HashMap map=new HashMap();
扫描仪键盘=新扫描仪(System.in);
受保护的中期项目(布尔追加)引发IOException、SecurityException{
超级();
this.append=append;
this.initialized=true;
}
公共中期项目(OutputStream out,布尔追加)引发IOException{
超级(出局);
this.append=append;
this.initialized=true;
this.dout=新数据输出流(out);
this.writeStreamHeader();
}
@凌驾
受保护的void writeStreamHeader()引发IOException{
如果(!this.initialized | | this.append)返回;
如果(dout!=null){
杜特·瑞特肖特(魔法之流);
dout.writeShort(流_版本);
}
}
如果你认为你误用了序列化:序列化是好是坏是另一回事,但应该是这样的:
List<Student> students = ... ;
try (OuputStream os = Files.newOutputStream(Paths.get("out"));
ObjectOutputStream oos = new ObjectOutputStream(os)) {
oos.writeObject(students);
}
请注意,字段是而不是静态的:序列化是关于序列化对象及其字段。静态字段不是任何实例的一部分
您也不必扩展ObjectOutputStream
类,或者如果扩展了类,则必须确保读取由ObjectOutputStream
实现编写的对象时,该对象与正在使用的ObjectInputStream
对称:
- 如果你写一个标题,你的
List<Student> students = ... ; try (OuputStream os = Files.newOutputStream(Paths.get("out")); ObjectOutputStream oos = new ObjectOutputStream(os)) { oos.writeObject(students); }
try (InputStream is = Files.newInputStream(Paths.get("out")); ObjectInputStream iis = new ObjectInputStream(is)) { List<Student> students = iis.readObject(students); }
class Student implements Serializable { private static final long serialVersionUID = 1L; private long id; private String fullName; private String address; private transient String wontBeExported; ... }