Java 为什么我会得到;StreamCorruptionException“;关于以下代码?
我试图编写一段代码,动态地尝试在不同点将多个对象序列化到单个文件中。代码如下:Java 为什么我会得到;StreamCorruptionException“;关于以下代码?,java,oop,debugging,serialization,stream,Java,Oop,Debugging,Serialization,Stream,我试图编写一段代码,动态地尝试在不同点将多个对象序列化到单个文件中。代码如下: import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class EmployeeManagementSystem { static Scanner sc = new Scanner(System.in); public static vo
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class EmployeeManagementSystem {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException {
List<Employee> objectEmployeeList= new ArrayList<Employee>();
int ch;
do {
ch = getChoice();
switch(ch) {
case 1:
Employee emp = getNewEmployee();
File file = new File("data.ser");
if(!file.exists()) {
System.out.println("Could ont create file");
file.createNewFile();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
//System.out.print(fis.read());
if(fis.read() != -1) {
ObjectInputStream ois = new ObjectInputStream(fis);
objectEmployeeList.clear();
Object obj = ois.readObject();
if(obj instanceof ArrayList<?>) {
ArrayList<?> al = (ArrayList<?>) obj;
if(al.size() > 0) {
for(int i = 0; i < al.size(); i++) {
Object o = al.get(i);
if(o instanceof Employee) {
Employee tempEmp = (Employee) o;
objectEmployeeList.add(tempEmp);
}
}
}
}
ois.close();
}
objectEmployeeList.add(emp);
fis.close();
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
System.out.println(" inside file");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(objectEmployeeList);
oos.flush();
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case 2:
System.out.println("----Report-----");
File file2= new File("data.ser");
if(!file2.exists()) {
System.out.println("Unable to create file");
}
FileInputStream fis2;
try {
fis2 = new FileInputStream(file2);
ObjectInputStream ois= new ObjectInputStream(fis2);
objectEmployeeList.clear();
Object obj = ois.readObject();
if(obj instanceof ArrayList<?>) {
ArrayList<?> al = (ArrayList<?>) obj;
if(al.size() > 0) {
for(int i = 0; i < al.size(); i++) {
Object o = al.get(i);
if(o instanceof Employee) {
Employee tempEmp = (Employee) o;
objectEmployeeList.add(tempEmp);
}
}
}
}
try {
Iterator<Employee> objectIterator= objectEmployeeList.iterator();
while(objectIterator.hasNext()) {
System.out.println(objectIterator.next().id + " "+objectIterator.next().name + " "+objectIterator.next().age + " "+objectIterator.next().salary);
}
} catch(Exception e) {
System.out.println("Exception occured" + e);
}
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("----End of Report-----");
break;
case 3:
System.out.println("Exiting the System");
break;
default:
System.out.println("Invalid Choice");
}
} while(ch != 3);
}
public static Employee getNewEmployee() {
int id;
String name;
int age;
double salary;
System.out.print("Enter Employee ID: ");
id = sc.nextInt();
System.out.print("Enter Employee name: ");
name = sc.next();
System.out.print("Enter Employee Age: ");
age = sc.nextInt();
System.out.print("Enter Employee Salary: ");
salary = sc.nextDouble();
Employee emp = new Employee(id, name, age, salary);
return emp;
}
public static int getChoice() {
System.out.println("Main Menu");
System.out.println("1. Add an Employee");
System.out.println("2. Display All");
System.out.println("3. Exit");
int ch = sc.nextInt();
return ch;
}
}
测试时我得到的输出是:
Main Menu
1. Add an Employee
2. Display All
3. Exit
1
Enter Employee ID: 12345
Enter Employee name: JohnDoe
Enter Employee Age: 32
Enter Employee Salary: 45000
Could ont create file
inside file
Main Menu
1. Add an Employee
2. Display All
3. Exit
2
----Report-----
Exception occuredjava.util.NoSuchElementException
----End of Report-----
Main Menu
1. Add an Employee
2. Display All
3. Exit
1
Enter Employee ID: 54321
Enter Employee name: JaneDoe
Enter Employee Age: 23
Enter Employee Salary: 50000
java.io.StreamCorruptedException: invalid stream header: ED000573
Main Menu
1. Add an Employee
2. Display All
3. Exit
at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966)
at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:405)
at EmployeeManagementSystem.main(EmployeeManagementSystem.java:33)
主菜单
1.添加员工
2.全部显示
3.出口
1.
输入员工ID:12345
输入员工姓名:JohnDoe
输入员工年龄:32
输入员工工资:45000
无法创建文件
内部文件
主菜单
1.添加员工
2.全部显示
3.出口
2.
----报告-----
异常发生djava.util.NoSuchElementException
----报告结束-----
主菜单
1.添加员工
2.全部显示
3.出口
1.
输入员工ID:54321
输入员工姓名:JaneDoe
输入员工年龄:23
输入员工工资:50000
java.io.StreamCorruptedException:无效的流头:ED000573
主菜单
1.添加员工
2.全部显示
3.出口
位于java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966)
位于java.base/java.io.ObjectInputStream。(ObjectInputStream.java:405)
位于EmployeeManagementSystem.main(EmployeeManagementSystem.java:33)
我无法理解为什么会发生这种情况。我还试图覆盖ObjectOutputStream类中的writeStreamHeader()
方法,但这也没有帮助。我是java编程新手,因此我非常感谢您的帮助。谢谢
Main Menu
1. Add an Employee
2. Display All
3. Exit
1
Enter Employee ID: 12345
Enter Employee name: JohnDoe
Enter Employee Age: 32
Enter Employee Salary: 45000
Could ont create file
inside file
Main Menu
1. Add an Employee
2. Display All
3. Exit
2
----Report-----
Exception occuredjava.util.NoSuchElementException
----End of Report-----
Main Menu
1. Add an Employee
2. Display All
3. Exit
1
Enter Employee ID: 54321
Enter Employee name: JaneDoe
Enter Employee Age: 23
Enter Employee Salary: 50000
java.io.StreamCorruptedException: invalid stream header: ED000573
Main Menu
1. Add an Employee
2. Display All
3. Exit
at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966)
at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:405)
at EmployeeManagementSystem.main(EmployeeManagementSystem.java:33)