我如何在Java中序列化包含对象的对象?

问题描述 投票:0回答:2

在我们的学期项目中,我们负责制作桌面游戏的桌面版本。我的角色是实现保存和加载方法。因此,我正在进行序列化。我正在尝试序列化包含其他对象的对象。为了可视化,我准备了一些示例类。

import java.io.Serializable;

public class Player  implements Serializable
{
   static int id;
   static String name;

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

import java.io.Serializable;
public class Team implements Serializable
{
   String name;
   transient Player c;
   int[] numbers;


   public Team(String name, String capt){
      this.name = name;
      this.c = new Player(capt);
   }

   public Player getC()
   {
      return c;
   }

   public String getName()
   {
      return name;
   }
}
public class test implements Serializable
{  
   public static void main(String args[])throws IOException 
   {
      Team t1 = new Team("Juventus", "Ronaldo");  
      Team t2 = new Team("Barcelona", "Messi");
      File file = createSave("1");
      save(file,t1,t2);
      Team[] teams = load(file);
      System.out.println("Team 1 is: " + teams[0].getName());  
      System.out.println("Team 2 is: " + teams[1].getName());
      System.out.println("Captain of team 1 is: " + teams[0].getC().name);
      System.out.println("Captain of team 2 is: " + teams[1].getC().name);


   }  
   private static File createSave(String gameId) throws IOException   
   { 
      File file = new File(gameId + ".txt");
      if (file.createNewFile())
      {
         System.out.println("File is created!");
      } 
      else 
      {
         System.out.println("File already exists.");
      }
      return file;     
   }

   private static void save(File file, Team t1, Team t2)throws IOException   
   { 
      FileOutputStream f = new FileOutputStream(file);
      ObjectOutputStream o = new ObjectOutputStream(f);
      o.writeObject(t1);
      o.writeObject(t2);
      o.close();
      f.close();

   }

   private static Team[] load(File file)throws IOException   
   { 

      try {

      Team[] teams = new Team[2];
      FileInputStream fi = new FileInputStream(file);
      ObjectInputStream oi = new ObjectInputStream(fi);
      Team team1 = (Team) oi.readObject();
      Team team2 = (Team) oi.readObject();
      teams[0] = team1;
      teams[1] = team2;
      oi.close();
      fi.close();
      return teams;



      } catch (FileNotFoundException e) {
         System.out.println("File not found");
      } catch (IOException e) {
         System.out.println("Error initializing stream");
      } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
    return null;
   }

}

总结代码部分:我创建了一个玩家类和一个包含玩家对象的团队类。在团队课中,有一种方法可以返回团队的队长。我创建了两个团队实例,第一个是尤文图斯,而队长是罗纳尔多。第二个是巴塞罗那,队长是梅西。然后,我使用ObjectOutputStream将这两个团队对象存储到txt文件中。在加载方法中,我使用ObjectInputStream加载了两个团队,并将它们作为数组返回。

[当我尝试到达两个团队对象的名称变量时,我可以。但是,我无法联系到球队的队长。仅最后创建的播放器对象可用。让我分享我的输出:

文件已存在。

第1队是:尤文图斯

第2队是:巴塞罗那

第一队的队长是:梅西

第2队的队长是:梅西

如输出所示,我只能到达最后创建的内部对象。球员罗纳尔多现在迷路了。如果您能帮助我,我将非常高兴。

java serialization save load
2个回答
0
投票

我总是建议使用GSON来序列化和反序列化对象。看一看:)


0
投票

首先,您需要练习并了解有关Java基础知识的更多信息。您所做的错误是在Player类中。您将名称设置为静态字段。你们所有的玩家只能有一个名字。

尝试像这样替换您的班级:

import java.io.Serializable;

public class Player implements Serializable {
    private int id;
    private String name;

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

    public String getName() {
        return name;
    }
}

团队课程中的下一个只是删除瞬变

Player c;

并且在主类中使用

System.out.println("Captain of team 1 is: " + teams[0].getC().getName());
System.out.println("Captain of team 2 is: " + teams[1].getC().getName());

现在您的代码应该可以工作了:)尚不完善,但这可以解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.