Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中加载/存储文件中的对象_Java_File_Load_Store - Fatal编程技术网

在Java中加载/存储文件中的对象

在Java中加载/存储文件中的对象,java,file,load,store,Java,File,Load,Store,我想将类中的一个对象存储在文件中,然后才能从该文件加载该对象。但是在某个地方我犯了一个错误,不知道在哪里。我能得到一些帮助吗 public class GameManagerSystem implements GameManager, Serializable { private static final long serialVersionUID = -5966618586666474164L; HashMap<Game, GameStatus> games;

我想将类中的一个对象存储在文件中,然后才能从该文件加载该对象。但是在某个地方我犯了一个错误,不知道在哪里。我能得到一些帮助吗

public class GameManagerSystem implements GameManager, Serializable {

    private static final long serialVersionUID = -5966618586666474164L;
    HashMap<Game, GameStatus> games;
    HashMap<Ticket, ArrayList<Object>> baggage;
    HashSet<Ticket> bookedTickets;
    Place place;


    public GameManagerSystem(Place place) {
        super();

        this.games = new HashMap<Game, GameStatus>();
        this.baggage = new HashMap<Ticket, ArrayList<Object>>();
        this.bookedTickets = new HashSet<Ticket>();
        this.place = place;
    }
    public static GameManager createManagerSystem(Game at) {
        return new GameManagerSystem(at);
    }

    public boolean store(File f) {
        try {
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(games);
            oos.writeObject(bookedTickets);
            oos.writeObject(baggage);
            oos.close();
            fos.close();
        } catch (IOException ex) {
            return false;
        }
        return true;
    }
    public boolean load(File f) {
        try {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
            this.games = (HashMap<Game,GameStatus>)ois.readObject();
            this.bookedTickets = (HashSet<Ticket>)ois.readObject();
                this.baggage = (HashMap<Ticket,ArrayList<Object>>)ois.readObject();
            ois.close();
            fis.close();
        } catch (IOException e) {
            return false;
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }
.
.
.
}


public class JUnitDemo {

    GameManager manager;

    @Before
    public void setUp() {
        manager = GameManagerSystem.createManagerSystem(Place.ENG);
    }

    @Test
    public void testStore() {
        Game g = new Game(new Date(), Teams.LIONS, Teams.SHARKS);
        manager.registerGame(g);
        File file = new File("file.ser");
        assertTrue(airport.store(file));
    }
}
公共类GameManager系统实现GameManager,可序列化{
私有静态最终长serialVersionUID=-5966618586666474164L;
HashMap游戏;
行李地图;
HashSet-bookedTickets;
地点;
公共游戏管理系统(Place){
超级();
this.games=新HashMap();
this.baggage=newhashmap();
this.bookedTickets=new HashSet();
这个地方=地方;
}
公共静态游戏管理器createManagerSystem(游戏位于){
返回新的GameManagerSystem(at);
}
公共布尔存储(文件f){
试一试{
FileOutputStream fos=新的FileOutputStream(f);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
oos.writeObject(游戏);
oos.writeObject(bookedTickets);
oos.书面对象(行李);
oos.close();
fos.close();
}捕获(IOEX异常){
返回false;
}
返回true;
}
公共布尔加载(文件f){
试一试{
FileInputStream fis=新的FileInputStream(f);
ObjectInputStream ois=新ObjectInputStream(fis);
this.games=(HashMap)ois.readObject();
this.bookedTickets=(HashSet)ois.readObject();
this.baggage=(HashMap)ois.readObject();
ois.close();
fis.close();
}捕获(IOE异常){
返回false;
}catch(classnotfounde异常){
返回false;
}
返回true;
}
.
.
.
}
公共类JUnitDemo{
游戏经理;
@以前
公共作废设置(){
manager=GameManagerSystem.createManagerSystem(Place.ENG);
}
@试验
公共void testStore(){
游戏g=新游戏(新日期(),Teams.LIONS,Teams.SHARKS);
注册经理姓名(g);
File File=新文件(“File.ser”);
assertTrue(airport.store(文件));
}
}

在关闭它之前,请尝试oos.flush()。

此问题的解决方案是,当您使用其他对象时,比如类
A
,将其放入一个类似
HashMap
的集合中,并希望序列化
HashMap
对象,然后为类
A
实现接口
Serializable
,如下所示:

class A implements Serializable {
}

...
    HashMap<Integer,A> hmap;
...
类实现了可序列化{
}
...
HashMap-hmap;
...
否则,该对象将无法序列化


我希望它现在能解决这个问题。

请记住,在序列化过程中,整个对象图是持久化的。例如,如果您有一些对GUI类的引用,您要么也必须使它们可序列化,要么将它们标记为“transient”,这样Java就不会序列化它们。

您看到的错误是什么?我很好奇。既然整个对象是可序列化的,为什么不执行oos.writeObject(此)?我得到NotSerializableException:(Game、GameStatus和Ticket实现可序列化吗?您是否尝试隔离每个writeObject以查看此异常来自何处?对象不可序列化,因此行李的典型内容是什么类型?所有输出流在关闭()之前都会执行flush()…至少实现良好的输出流,这包括JRE中的内容。