简体   繁体   English

我无法从文件中读取其他对象:java.io.streamcorruptedexception:无效类型代码:ac

[英]I can't read the other objects from file: java.io.streamcorruptedexception: invalid type code: ac

I am trying to read all the saved objects from the file, but I am only able to read the first one and then an exception appears ( java.io.streamcorruptedexception: invalid type code: ac )我正在尝试从文件中读取所有保存的对象,但我只能读取第一个对象,然后出现异常( java.io.streamcorruptedexception: invalid type code: ac

Here is My Code:这是我的代码:

public void loadfile()
{
try{
FileInputStream file = new FileInputStream("accounts.dat");
ObjectInputStream inputfile = new ObjectInputStream(file);
boolean endoffile=false;
while(!endoffile){
try{
   
accounts2.add((Account) inputfile.readObject());
}catch(EOFException e){
endoffile=true;
}
catch(Exception f){
JOptionPane.showMessageDialog(null, f.getMessage());
}
}
inputfile.close();
   
}catch(IOException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}

}
public void savefile(){
try{
FileOutputStream file = new FileOutputStream("accounts.dat",true);
ObjectOutputStream outputfile = new ObjectOutputStream(file);
for (int i =0 ; i < accounts.size();i++)
{


outputfile.writeObject(accounts.get(i));
}
    outputfile.close();
 JOptionPane.showMessageDialog(null, "Succesfully Registered");
 this.dispose();
    
    
    
}catch(IOException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}

If you try to put the inputfile.readObject() value into a variable then you giving a new value to it in every iteration?如果您尝试将 inputfile.readObject() 值放入变量中,那么您会在每次迭代中为其赋予一个新值吗?

I think that's because there are 2 ObjectOutputStream, and (I'm not sure about this), it's possible that sometimes the first one is not/bad closed... Maybe try to use the same OOS?我认为那是因为有 2 个 ObjectOutputStream,并且(我不确定),有时第一个可能没有/坏关闭......也许尝试使用相同的 OOS?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.StreamCorruptedException:无效的类型代码:68 - java.io.StreamCorruptedException: invalid type code: 68 java.io.StreamCorruptedException:无效的类型代码:04 - java.io.StreamCorruptedException: invalid type code: 04 异常:java.io.StreamCorruptedException:无效的类型代码:00 - Exception: java.io.StreamCorruptedException: invalid type code: 00 反序列化问题-java.io.StreamCorruptedException:无效的类型代码:00 - Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00 java.io.StreamCorruptedException:无效类型代码:00 - java.io.StreamCorruptedException: invalid type code: 00 java.io.StreamCorruptedException:无效的类型代码:自定义序列化过程时为3F - java.io.StreamCorruptedException: invalid type code: 3F when I customize serialization process java.io.StreamCorruptedException:是否存在带有说明的无效代码类型的列表? - java.io.StreamCorruptedException: Is there a list of Invalid code types with description? 多人游戏(Java套接字java.io.StreamCorruptedException:无效的类型代码:00) - Multiplayer game (Java socket java.io.StreamCorruptedException: invalid type code: 00) java.io.StreamCorruptedException:无效的类型代码:4C-复制流tomcat - java.io.StreamCorruptedException: invalid type code: 4C - replicationstream tomcat java.io.StreamCorruptedException:无效 stream Z099FB9953400031C7549FE:AC - java.io.StreamCorruptedException: invalid stream header: AC3F0005
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM