Java序列化不工作,由于IDE而出现中断

Java序列化不工作,由于IDE而出现中断,java,git,serialization,Java,Git,Serialization,编辑:错误是由Netbeans而不是代码引起的。已编辑Post以显示自删除Git文件以来的所有代码 我在学校有一个小组项目,设计一个塔防游戏,我一直在尝试添加序列化 代码如下: public class Serialization implements Serializable { private static FileOutputStream file; private static ObjectOutputStream write; public static boolean chec

编辑:错误是由Netbeans而不是代码引起的。已编辑Post以显示自删除Git文件以来的所有代码


我在学校有一个小组项目,设计一个塔防游戏,我一直在尝试添加序列化

代码如下:

public class Serialization implements Serializable {

private static FileOutputStream file;
private static ObjectOutputStream write;

public static boolean checkFile(String name){
    boolean check = false;
    check = new File("log",name+".ser").isFile();
    return check;
}

public static void createFile(String name) {
    try {
        file = new FileOutputStream("log/"+name+".ser");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static ObjectOutputStream openFile(String name) {
    try {
        file = new FileOutputStream("log/"+name+".ser");
        write = new ObjectOutputStream(file);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    }
    return write;
}

public static void addLine(ObjectOutputStream con,Object data) {
    try {
        con.writeObject(data);
    } catch (IOException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void readLine(String name){
    FileInputStream fileIn = null;
    try {
        fileIn = new FileInputStream("log/"+name+".ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        System.out.println(in.readObject());
        in.close();
        fileIn.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            fileIn.close();
        } catch (IOException ex) {
            Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public static void closeFile(ObjectOutputStream con){
    try {
        con.close();
    } catch (IOException ex) {
        Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}
我最终放弃了它有两个原因。一个是杀伤力过大(试图使用一个处理多个序列化文件的中心类),另一个是我无法让它与我将要讨论的内容一起工作

我真正想要的是序列化enemyArray。这保存了游戏中所有活着的敌人及其所有信息(统计数据)。如果向下滚动到第192行,这就是我序列化尝试的剩余部分:

public void serialize(){
    if (Serialization.checkFile("test")==false){
        Serialization.createFile("test");
    }
    ObjectOutputStream connection = Serialization.openFile("test");
    for (int x=0;x<enemyArray.length;x++){
            if (enemyArray[x]!=null){
                Serialization.addLine(connection,enemyArray[x]);
            }
        }
    //Serialization.addLine(connection,enemyArray);
    Serialization.closeFile(connection);
    enemyArray = null;
    Serialization.readLine("test");
    //System.out.println(enemyArray[0].id);
//      try {
//          FileOutputStream fileOut = new FileOutputStream("log/test.ser");
//          ObjectOutputStream out = new ObjectOutputStream(fileOut);
//          out.writeObject(enemyArray);
//          out.writeUTF(t);
//          // Do work here
//          for (int x=0;x<enemyArray.length;x++){
//              if (enemyArray[x]!=null){
//                  Enemy tmp = enemyArray[x];
//                  System.out.println(tmp+" >>> "+enemyArray[x]);
//                  out.writeObject(tmp);
//              }
//          }
//          out.close();
//          fileOut.close();
//      } catch (FileNotFoundException ex) {
//          Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
//      } catch (IOException ex) {
//          Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
//      }
}
}
public void serialize(){
if(Serialization.checkFile(“test”)==false){
Serialization.createFile(“test”);
}
ObjectOutputStream连接=Serialization.openFile(“测试”);

对于(int x=0;x请在关闭输出流之前尝试执行out.flush();b。

在计算机实验室与我的小组一起进行故障排除后,我发现了错误。希望这可以帮助任何遇到我的情况或类似Java文件读写问题的人

My IDE在动态创建文件后,会在文件和项目列表中显示该文件,但由于该文件中填充了序列化数据,并且是在项目打开并运行后创建的,因此在文件中实际存在数据时,它只会显示空文件。字符串数据很好,因此它可能只是某些数据类型(像序列化对象)触发此错误

我的代码的几个不同版本都是正确的,但它们似乎都被破坏了,因为一个空文件被返回到IDE。解决方案有两个方面:

  • 通过文件资源管理器手动检查它。它将显示一个大小,可以在其他编辑器中打开,但在运行程序的IDE中可能仍然有缺陷
  • 关闭IDE,重新打开时,至少在Netbeans情况下,应该正确更新文件

  • 类似地,Netbeans可能会一起显示空文件,而不是为您提供选择文件编码的选项。在这种情况下,只需在浏览器中查看文件,如果大小大于0,您就知道您的代码正常。这纯粹是一个IDE错误。

    您能对否决票发表意见吗。我今晚9点在实验室告诉您,但是如果需要,可以稍后添加代码和我的示例。查看我选择的答案。这是一个IDE错误,花费了我几个小时或重新编码和排除故障。