java - StreamCorruptedException:无效的流标头:从文件中读取对象时出现 79737200
问题描述
我创建了一个客户端相似性,其中客户端注册了一个帐户(创建了一个对象),该帐户存储在一个文件中。
对象根据需要写入文件,我重写了writeStreamHeader()
方法。但是当我尝试阅读它们时,它们的文件会引发异常。
在此处将对象写入文件。
public static void saveAccaunt(LoginAndPass gamers) {
boolean b = true;
FileInputStream fis = null;
try{
fis = new FileInputStream("student.ser");
fis.close();
}
catch (FileNotFoundException e)
{
b = false;
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fileOutputStream = new FileOutputStream("student.ser",true);
ObjectOutputStream os = null;
if(b = true){
os = new AppendingObjectOutputStream(fileOutputStream);
System.out.println("Объект добавлен!");
}else {
os = new ObjectOutputStream(fileOutputStream);
System.out.println("Создан");
}
os.writeObject(gamers);
os.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream("student.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
test = new ArrayList<>();
while (true){
test.add(objectInputStream.readObject());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(test.get(0));
}
这是引发的异常的错误日志:
java.io.StreamCorruptedException:无效的流标头:79737200
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
at java.io.ObjectInputStream.(ObjectInputStream.java:358)
at Registratsiya.AllGamers.main(AllGamers.java :48)
Registratsiya.AllGamers.main(AllGamers.java:61) 的线程“main”java.lang.NullPointerException 中的异常
解决方案
推荐阅读
- javascript - 在同一个点击事件上触发多个下拉菜单
- spring-boot - JSON字段在Spring Boot中反序列化为小写
- java - 使用数据类型命名变量来描述操作
- post - Asp.Net Web API 中的 PUT 与 POST?
- python - 无法将工具栏从顶部/底部移动到左/右,反之亦然
- r - 在 Shiny 中输入信息后渲染图形
- github-actions - 从 GitHub 操作与 Bitbucket 工作流调用时,相同的 shell 脚本会产生不同的结果
- javascript - 为什么波浪号 ~ 在 Javascript querySelector 中不起作用但在 CSS 中起作用?
- common-lisp - 编译 Lambda 表达式时 SBCL 中的微妙编译器警告
- elixir - elixir phoenix liveview - 通过套接字传递用户 ID