【发布时间】:2020-07-28 04:53:30
【问题描述】:
我有一个名为“Settings”的类,它有一个“UniverseOut”数组。
private static final long serialVersionUID = 2929431155275389116L;
private String projectName = "last-project";
private UniverseOut[] universeOut = new UniverseOut[unicastLimit];
当我通过命令更改设置类中的“项目名称”时,它会更改它,当我保存项目并重新启动应用程序时,它将完美读取名称而不会出现任何错误。
但 UniverseOut 数组根本没有被保存,它只是说数组来自该类型,但值/对象本身没有保存。
public class UniverseOut implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1326425548448853224L;
private static final int max_ip = 10;
private static String[] ip = new String[max_ip];
}
这是我如何将对象保存为 XML 文件的代码:
String path = System.getProperty("user.dir");
FileOutputStream fos = new FileOutputStream(path + "\\" + projectname + ".project");
XMLEncoder xml = new XMLEncoder(fos);
xml.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception e) {
System.out.println("Exception! :" + e.toString());
}
});
xml.writeObject(getSettings());
xml.close();
fos.close();
fos = new FileOutputStream(path + "\\last-project.project");
xml = new XMLEncoder(fos);
xml.writeObject(getSettings());
xml.close();
fos.close();
文件正在创建中,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<java version="13.0.2" class="java.beans.XMLDecoder">
<object class="preferences.Settings" id="Settings0">
<void property="projectName">
<string>lol</string>
</void>
<void property="universeOut">
<void index="0">
<object class="preferences.UniverseOut"/>
</void>
<void index="1">
<object class="preferences.UniverseOut"/>
</void>
</void>
</object>
</java>
我非常有信心这个数组应该包含一些东西,但它没有保存。
【问题讨论】:
标签: java arrays xml object serializable