Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Serialization - Fatal编程技术网

Java中的数据反序列化

Java中的数据反序列化,java,serialization,Java,Serialization,我试图从数组列表中读取玩家数据,但经过2小时的搜索和实验,我迷路了 下面是反序列化代码。虽然有注释,但基本上应该有一个包含播放器对象的序列化ArrayList的文件。我想反序列化该数据并将其存储在savedPlayers中,然后将savedPlayers用于一些事情 我做错了什么? import java.io.*; import java.util.*; public class RoofRunnerApp { private Scanner in; // Sca

我试图从数组列表中读取玩家数据,但经过2小时的搜索和实验,我迷路了

下面是反序列化代码。虽然有注释,但基本上应该有一个包含播放器对象的序列化ArrayList的文件。我想反序列化该数据并将其存储在savedPlayers中,然后将savedPlayers用于一些事情

我做错了什么?

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

public class RoofRunnerApp {
 private Scanner in;              // Scanner to take playerName input.
 private RoofRunnerGame gameTime;
 private String filename = "gameData.txt";

 public static void main (String[] args) {
  System.out.println("WELCOME TO *ROOF RUNNER* \n");    // Welcomes user with game title.
  RoofRunnerApp myGame = new RoofRunnerApp();
 }

public RoofRunnerApp() {
 // 1) Read in name from user.
  in = new Scanner(System.in);
  System.out.print("Please enter your name: ");
  String playerName = in.next();   
 // 2) Create Player arrayList.   
  ArrayList<Player> savedPlayers = new ArrayList<Player>();
 // 3) Read in Serialized Data from file.
  try {
     ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename));    // Add file name.
     savedPlayers = (ArrayList<Player>)input.readObject(); 
     input.close();
 } catch( Exception e ) {
     e.printStackTrace();
     return;
 }
  Player thisPlayer;     //thisPlayer is passed to Game class.
 // 4) if arraylist != empty -> Check for playerName and inputName match.
  ////// If there's a match -> Set thisPlayer equal to the savedPlayer, using the Player constructor.
  if(savedPlayers.length() != 0)
   for(int i = 0; i < savedPlayers.length(); i++)
    if(playerName.equals(savedPlayers.getName())){
     thisPlayer = new Player(savedPlayers[i]);
     break;
    }
  }
 else
  thisPlayer = new Player(playerName); // If no match -> Set thisPlayer to new player with playerName. 

 RoofRunnerGame gameTime = new RoofRunnerGame(thisPlayer);
}
import java.io.*;
导入java.util.*;
公共类屋顶{
中的专用扫描仪;//用于获取playerName输入的扫描仪。
私人屋顶奔跑者游戏时间;
私有字符串filename=“gameData.txt”;
公共静态void main(字符串[]args){
System.out.println(“欢迎使用*ROOF RUNNER*\n”);//欢迎使用游戏标题的用户。
rootrunnerapp myGame=新的rootrunnerapp();
}
公共屋顶{
//1)从用户处读入名称。
in=新扫描仪(System.in);
System.out.print(“请输入您的姓名:”);
字符串playerName=in.next();
//2)创建播放器阵列列表。
ArrayList savedPlayers=new ArrayList();
//3)从文件中读入序列化数据。
试一试{
ObjectInputStream输入=新ObjectInputStream(新文件输入流(文件名));//添加文件名。
savedPlayers=(ArrayList)input.readObject();
input.close();
}捕获(例外e){
e、 printStackTrace();
返回;
}
Player thisPlayer;//thisPlayer被传递到游戏类。
//4)如果arraylist!=空->检查playerName和inputName是否匹配。
//////如果存在匹配->使用播放器构造函数将此播放器设置为等于savedPlayer。
如果(savedPlayers.length()!=0)
对于(int i=0;i将此玩家设置为具有playerName的新玩家。
屋顶跑步者游戏时间=新的屋顶跑步者游戏(该玩家);
}

我知道我不应该回答,但它不允许我编辑

更新/问题

我还没有序列化任何数据。现在我正在尝试设置反序列化(之后我将添加序列化)。但是,在Eclipse中,当我尝试上述操作时,会收到一些错误消息,说savedPlayers不是arrayList,并且length()对它不起作用

这是课程代码。(底部的代码仍在编写中。我的错误发生在第33-37行:

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

public class RoofRunnerApp {
  private Scanner in; // Scanner to take playerName input.
  private RoofRunnerGame gameTime;
  private String filename = "gameData.txt"; // File to hold Serialized Player data.

  public static void main (String[] args) {
    System.out.println("WELCOME TO *ROOF RUNNER* \n"); // Welcomes user with game title.
    RoofRunnerApp myGame = new RoofRunnerApp();
  }

  public RoofRunnerApp() {
    // 1) Read in name from user.
    in = new Scanner(System.in);
    System.out.print("Please enter your name: ");
    String playerName = in.next();   
    // 2) Create Player arrayList.   
    ArrayList<Player> savedPlayers = new ArrayList<Player>();
    // 3) Read in Serialized Data from file.
    try {
      ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename)); // Add file name.
      savedPlayers = (ArrayList<Player>)input.readObject();
      input.close();
    } catch( Exception e ) {
      e.printStackTrace();
      return;
    }
    Player thisPlayer; //thisPlayer is passed to Game class.
    // 4) if arraylist != empty -> Check for playerName and inputName match.
    ////// If there's a match -> Set thisPlayer equal to the savedPlayer, using the Player constructor.
    if(savedPlayers.length() != 0)
      for(int i = 0; i < savedPlayers.length(); i++)
        if(playerName.equals(savedPlayers[i].getName())){
          thisPlayer = new Player(savedPlayers[i]);
          break;
        }
    }
    else
      thisPlayer = new Player(playerName); // If no match -> Set thisPlayer to new player with playerName. 

    RoofRunnerGame gameTime = new RoofRunnerGame(thisPlayer);

    // After:
    // Exit code in RRG
    // 1) Add current Player to current ArrayList.
    /*
    // Create/Return player objects.
    Player p = new Player(playerObject)
    savedPlayers.add(p);
    ///// Save data to file.
    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream( new FileOutputStream( "myfile") );  // Add filename.
      out.writeObject( savedPlayer ); //write the entire ArrayList
      out.close();
    } catch( IOException e ) {
      e.printStackTrace();
      return;
    }
    */

  }

}
import java.io.*;
导入java.util.*;
公共类屋顶{
中的专用扫描仪;//用于获取playerName输入的扫描仪。
私人屋顶奔跑者游戏时间;
私有字符串filename=“gameData.txt”;//保存序列化玩家数据的文件。
公共静态void main(字符串[]args){
System.out.println(“欢迎使用*ROOF RUNNER*\n”);//欢迎使用游戏标题的用户。
rootrunnerapp myGame=新的rootrunnerapp();
}
公共屋顶{
//1)从用户处读入名称。
in=新扫描仪(System.in);
System.out.print(“请输入您的姓名:”);
字符串playerName=in.next();
//2)创建播放器阵列列表。
ArrayList savedPlayers=new ArrayList();
//3)从文件中读入序列化数据。
试一试{
ObjectInputStream输入=新ObjectInputStream(新文件输入流(文件名));//添加文件名。
savedPlayers=(ArrayList)input.readObject();
input.close();
}捕获(例外e){
e、 printStackTrace();
返回;
}
Player thisPlayer;//thisPlayer被传递到游戏类。
//4)如果arraylist!=空->检查playerName和inputName是否匹配。
//////如果存在匹配->使用播放器构造函数将此播放器设置为等于savedPlayer。
如果(savedPlayers.length()!=0)
对于(int i=0;i将此玩家设置为具有playerName的新玩家。
屋顶跑步者游戏时间=新的屋顶跑步者游戏(该玩家);
//之后:
//RRG中的退出代码
//1)将当前播放器添加到当前ArrayList。
/*
//创建/返回播放器对象。
玩家p=新玩家(玩家对象)
savedPlayers.add(p);
/////将数据保存到文件。
ObjectOutputStream out=null;
试一试{
out=newobjectoutputstream(newfileoutputstream(“myfile”);//添加文件名。
writeObject(savedPlayer);//写入整个ArrayList
out.close();
}捕获(IOE异常){
e、 printStackTrace();
返回;
}
*/
}
}

我知道我不应该回答,但它不允许我编辑

更新/问题

我还没有序列化任何数据。现在我正在尝试设置反序列化(之后我将添加序列化)。但是,在Eclipse中,当我尝试上述操作时,会收到一些错误消息,说savedPlayers不是arrayList,并且length()对它不起作用

这是课程代码。(底部的代码仍在编写中。我的错误发生在第33-37行:

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

public class RoofRunnerApp {
  private Scanner in; // Scanner to take playerName input.
  private RoofRunnerGame gameTime;
  private String filename = "gameData.txt"; // File to hold Serialized Player data.

  public static void main (String[] args) {
    System.out.println("WELCOME TO *ROOF RUNNER* \n"); // Welcomes user with game title.
    RoofRunnerApp myGame = new RoofRunnerApp();
  }

  public RoofRunnerApp() {
    // 1) Read in name from user.
    in = new Scanner(System.in);
    System.out.print("Please enter your name: ");
    String playerName = in.next();   
    // 2) Create Player arrayList.   
    ArrayList<Player> savedPlayers = new ArrayList<Player>();
    // 3) Read in Serialized Data from file.
    try {
      ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename)); // Add file name.
      savedPlayers = (ArrayList<Player>)input.readObject();
      input.close();
    } catch( Exception e ) {
      e.printStackTrace();
      return;
    }
    Player thisPlayer; //thisPlayer is passed to Game class.
    // 4) if arraylist != empty -> Check for playerName and inputName match.
    ////// If there's a match -> Set thisPlayer equal to the savedPlayer, using the Player constructor.
    if(savedPlayers.length() != 0)
      for(int i = 0; i < savedPlayers.length(); i++)
        if(playerName.equals(savedPlayers[i].getName())){
          thisPlayer = new Player(savedPlayers[i]);
          break;
        }
    }
    else
      thisPlayer = new Player(playerName); // If no match -> Set thisPlayer to new player with playerName. 

    RoofRunnerGame gameTime = new RoofRunnerGame(thisPlayer);

    // After:
    // Exit code in RRG
    // 1) Add current Player to current ArrayList.
    /*
    // Create/Return player objects.
    Player p = new Player(playerObject)
    savedPlayers.add(p);
    ///// Save data to file.
    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream( new FileOutputStream( "myfile") );  // Add filename.
      out.writeObject( savedPlayer ); //write the entire ArrayList
      out.close();
    } catch( IOException e ) {
      e.printStackTrace();
      return;
    }
    */

  }

}
import java.io.*;
导入java.util.*;
公共类屋顶{
中的专用扫描仪;//用于获取playerName输入的扫描仪。
私人屋顶奔跑者游戏时间;
私有字符串filename=“gameData.txt”;//保存序列化玩家数据的文件。
公共静态void main(字符串[]args){
System.out.println(“欢迎使用*ROOF RUNNER*\n”);//欢迎使用游戏标题的用户。
rootrunnerapp myGame=新的rootrunnerapp();
}
公共屋顶{
//1)从用户处读入名称。
in=新扫描仪(System.in);
System.out.print(“请输入您的姓名:”);
字符串playerName=in.next();
//2)创建播放器阵列列表。
ArrayList savedPlayers=new ArrayList();
for (Player player : players) {
  // do something with each Player
}
import java.io.*;
import java.util.*;

public class RoofRunnerApp {
    private Scanner in;                                                     // Scanner to take playerName input.
    private RoofRunnerGame gameTime;
    private String filename = "gameData.txt";                           // File to hold Serialized Player data.

    public static void main (String[] args) {
        System.out.println("WELCOME TO *ROOF RUNNER* \n");              // Welcomes user with game title.
        RoofRunnerApp myGame = new RoofRunnerApp();
    }

public RoofRunnerApp() {
    // 1) Read in name from user.
    in = new Scanner(System.in);
    System.out.print("Please enter your name: ");
    String playerName = in.next();      
    // 2) Create Player arrayList.   
    ArrayList<Player> savedPlayers = new ArrayList<Player>();
    // 3) Read in Serialized Data from file.
    try {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename));             // Add file name.
        savedPlayers = (ArrayList<Player>)input.readObject(); 
        input.close();
    } catch( Exception e ) {
        e.printStackTrace();
        return;
    }
    boolean notFound = true;
    Player thisPlayer;                  //thisPlayer is passed to Game class.
    // 4) if arraylist != empty -> Check for playerName and inputName match.
    ////// If there's a match -> Set thisPlayer equal to the savedPlayer, using the Player constructor.
    if(savedPlayers.size() != 0){
        for(int i = 0; i < savedPlayers.size(); i++){
            if(playerName.equals(savedPlayers.get(i).getName())){
                thisPlayer = new Player(savedPlayers.get(i));
                notFound = false;
                break;
            }
        }
    }
    if(savedPlayers.size() == 0 || notFound)
        thisPlayer = new Player(playerName);    // If no match -> Set thisPlayer to new player with playerName. 

    RoofRunnerGame gameTime = new RoofRunnerGame(thisPlayer);
}