
java 反序列化报错 提示:java.io.StreamCorruptedException 异常
publicvoidview(){//TODOAuto-generatedmethodstubSystem.out.println("请输入要操作的文件路径");Scan...
public void view() {
// TODO Auto-generated method stub
System.out.println("请输入要操作的文件路径");
Scanner scan = new Scanner(System.in);
String fileUrl = scan.nextLine();
File file = new File(fileUrl);
impl emp = new impl();
if (file.exists()) {
System.out.println("该文件已经找到");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
while (ois.readObject() != null) {
ArrayList<impl> arr = (ArrayList<impl>) ois.readObject();
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i).getName());
System.out.println(arr.get(i).getBirthDate());
System.out.println(arr.get(i).getCreateDate());
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
控制台中:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at neu.impl.view(impl.java:109)
at neu.test.main(test.java:28) 展开
// TODO Auto-generated method stub
System.out.println("请输入要操作的文件路径");
Scanner scan = new Scanner(System.in);
String fileUrl = scan.nextLine();
File file = new File(fileUrl);
impl emp = new impl();
if (file.exists()) {
System.out.println("该文件已经找到");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
while (ois.readObject() != null) {
ArrayList<impl> arr = (ArrayList<impl>) ois.readObject();
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i).getName());
System.out.println(arr.get(i).getBirthDate());
System.out.println(arr.get(i).getCreateDate());
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
控制台中:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at neu.impl.view(impl.java:109)
at neu.test.main(test.java:28) 展开
展开全部
感觉是你while语句那块的问题,while(ois.readObject() != null)这条语句表明从对象流中读取一个对象,此时对象流已经从文件中读取了字节序列并且创建了一个对象的实例。而你又在while语句中再次调用ois.readObject(),对象流会再次读取文件中的字节序列去反序列化对象,如果文件已经到了EOF,就会出现问题。感觉ObjectInputStream还是别用在while循环中
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询