使用 Java Serializable 保存第二个对象的数组

Save Array of 2nd Object with Java Serializable

我有一个名为 "Settings" 的 class,它有一个数组 "UniverseOut"。

private static final long serialVersionUID = 2929431155275389116L;

private String projectName = "last-project";

private UniverseOut[] universeOut = new UniverseOut[unicastLimit];

当我通过命令更改设置 class 中的 "projectName" 时,它会更改它,当我保存项目并重新启动应用程序时,它会完美读取名称而不会出现任何错误。

但 UniverseOut 数组根本没有被保存,它只是说该数组来自该类型,但 values/object 本身没有被保存。

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>

我非常有信心这个数组应该包含一些东西,但它没有被保存。

如果UniverseOut没有任何要序列化的非静态字段,那么就没有什么可以保存的,因为statics are implicitly transient