Java 序列化无效,由于 IDE 而出现损坏
Java Serialization not working, appears broken because of IDE
编辑: 该错误是由 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);
}
}
}
我最终放弃它有两个原因。一个是矫枉过正(试图有一个中央 class 来处理多个序列化文件),两个我无法让它与我将要谈论的内容一起工作。
我真正想要的是序列化 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);
// }
}
}
它将创建文件但不会向其中保存任何内容。 代码有什么问题? 根据我的研究,enemyArray 不应该是静态的,所以我删除了它,但仍然没有变化。
尝试做一个 out.flush();在关闭输出流之前。
我和我的小组在计算机实验室进行了一些故障排除后发现了错误。希望这可以帮助任何发现自己处于我的情况或类似 Java 文件读/写问题的人。
我的IDE,Netbeans,在文件和项目列表中显示的文件是动态创建的,但是因为填充了序列化数据,是在项目打开后创建的, 运行 当实际上文件中确实有数据时,它只会显示一个空文件。字符串数据很好,因此可能只有某些数据类型(如序列化对象)会触发此错误。
我的代码的几个不同版本都是正确的,但它们似乎都损坏了,因为一个空文件被返回到 IDE。解决方案有两个:
- 通过文件资源管理器手动检查它。它将显示一个大小并可以在另一个编辑器中打开,但在 IDE 您 运行 中的程序中可能仍然存在错误。
- 关闭您的 IDE 并在重新打开时,至少在 Netbeans 的情况下,文件应该正确更新。
类似地,Netbeans 可能只是一直显示空文件,而不是为您提供 select 文件编码选项。在这种情况下,只需在您的资源管理器中查看文件,如果大小大于 0,您就知道您的代码没问题。这纯粹是一个 IDE 错误。
编辑: 该错误是由 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);
}
}
}
我最终放弃它有两个原因。一个是矫枉过正(试图有一个中央 class 来处理多个序列化文件),两个我无法让它与我将要谈论的内容一起工作。
我真正想要的是序列化 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);
// }
}
}
它将创建文件但不会向其中保存任何内容。 代码有什么问题? 根据我的研究,enemyArray 不应该是静态的,所以我删除了它,但仍然没有变化。
尝试做一个 out.flush();在关闭输出流之前。
我和我的小组在计算机实验室进行了一些故障排除后发现了错误。希望这可以帮助任何发现自己处于我的情况或类似 Java 文件读/写问题的人。
我的IDE,Netbeans,在文件和项目列表中显示的文件是动态创建的,但是因为填充了序列化数据,是在项目打开后创建的, 运行 当实际上文件中确实有数据时,它只会显示一个空文件。字符串数据很好,因此可能只有某些数据类型(如序列化对象)会触发此错误。
我的代码的几个不同版本都是正确的,但它们似乎都损坏了,因为一个空文件被返回到 IDE。解决方案有两个:
- 通过文件资源管理器手动检查它。它将显示一个大小并可以在另一个编辑器中打开,但在 IDE 您 运行 中的程序中可能仍然存在错误。
- 关闭您的 IDE 并在重新打开时,至少在 Netbeans 的情况下,文件应该正确更新。
类似地,Netbeans 可能只是一直显示空文件,而不是为您提供 select 文件编码选项。在这种情况下,只需在您的资源管理器中查看文件,如果大小大于 0,您就知道您的代码没问题。这纯粹是一个 IDE 错误。