gpt4 book ai didi

java - 当我提取对象输入/输出流时出现 EOFException

转载 作者:行者123 更新时间:2023-12-02 05:36:23 25 4
gpt4 key购买 nike

我使用ObjectInput/Output来初始化名为temp的hashmap,并将名为map的hashmap的所有条目初始化为new,然后使用OutputStream将其保存为文件格式为.ser这工作完美...

import java.io.*;
import java.util.HashMap;

public class PlayerInfo implements Serializable {

ObjectOutputStream out;
ObjectInputStream in;
File userData =new File("path.ser");
HashMap map ;
HashMap temp;
private Integer ID;
String name ;
boolean isItNull =false;

public static void main(String[] args) {
new PlayerInfo();
}
PlayerInfo(){
try {
initializeHashMap();

}catch (Exception e){
e.printStackTrace();
}

}
@SuppressWarnings("unchecked")
private void initializeHashMap(){

try {
//initialize ObjectInputStream in same method when I use it and close it then
in =new ObjectInputStream(new FileInputStream(userData));

if (isItNull){
temp =new HashMap<Integer,PlayerInfo>();

}else {
map =new HashMap<Integer,PlayerInfo>();
temp = (HashMap<Integer, PlayerInfo>) in.readObject();
in.close();
}
}catch (Exception e){
isItNull =true;
initializeHashMap();
}

}

private void getInfo(){

System.out.println("Ok we are in get info so write your ID:-");
int id = 10;
}

@SuppressWarnings("unchecked")
private void createInfo()throws IOException{
//same here initialize ObjectOutputStreamin same method when I use it and close it then
out =new ObjectOutputStream(new FileOutputStream(userData));

System.out.println("Ok we are in create info so write your ID:-");
ID =10;
String scnS ="Mohammed";
System.out.println("Write your name");
map.put(ID,new PlayerInfo(scnS));
temp.putAll(map);

System.out.println("Saving....");
out.writeObject(temp);
out.close();
}

public PlayerInfo(String name){
this.name =name;
}


}

但这会抛出 EFOException


import java.io.*;
import java.util.HashMap;

public class PlayerInfo implements Serializable {

ObjectOutputStream out;
ObjectInputStream in;
File userData =new File("path.ser");
HashMap map ;
HashMap temp;
private Integer ID;
String name ;
boolean isItNull =false;

public static void main(String[] args) {

new PlayerInfo();

}
PlayerInfo(){
try {
openTheOutPutObjectStreamer();
openTheInPutObjectStreamer();
initializeHashMap();

}catch (Exception e){
e.printStackTrace();
}

}
//here I initialize it in separated method
private void openTheOutPutObjectStreamer()throws IOException{
out =new ObjectOutputStream(new FileOutputStream(userData));

}
//same here I initialize it in separated method

private void openTheInPutObjectStreamer()throws IOException{

in =new ObjectInputStream(new FileInputStream(userData));

}

@SuppressWarnings("unchecked")
private void initializeHashMap(){

try {

if (isItNull){
temp =new HashMap<Integer,PlayerInfo>();

}else {
map =new HashMap<Integer,PlayerInfo>();
temp = (HashMap<Integer, PlayerInfo>) in.readObject();
in.close();
}
}catch (Exception e){
isItNull =true;
initializeHashMap();
}

}

@SuppressWarnings("unchecked")
private void createInfo()throws IOException{

System.out.println("Ok we are in create info so write your ID:-");
ID =10;
String scnS ="Mohammed";
System.out.println("Write your name");
map.put(ID,new PlayerInfo(scnS));
temp.putAll(map);

System.out.println("Saving....");
out.writeObject(temp);
out.close();
}

public PlayerInfo(String name){
this.name =name;
}

}

如果您看到它,区别只是将对象输入/输出分离到方法并调用它们很抱歉我是这个网站的新手我对 IO 了解不多,但似乎无法将其分离为方法并调用它?

最佳答案

问题是,在你的第一个代码中,你(正确地)打开一个输入流,使用它,然后在对同一个文件执行任何其他操作之前关闭它,但在你的第二个代码版本中,你还在同一个文件上打开输出流读取它并且输出流将标记(读取或写入的位置)放在文件末尾,因此当您使用输入流时,您会收到文件结束错误。

将代码更改为此应该可以工作

openTheInPutObjectStreamer();
initializeHashMap();
openTheOutPutObjectStreamer();

关于java - 当我提取对象输入/输出流时出现 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56163467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com