简体   繁体   中英

Save Array of 2nd Object with Java Serializable

i've got a class called "Settings" which has an array of "UniverseOut".

private static final long serialVersionUID = 2929431155275389116L;

private String projectName = "last-project";

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

When i change the "projectName" in the settings class via a command, it changes it and when i save the project, and restart the application it will read the name perfectly without any mistakes.

But the UniverseOut Array isnt being saved at all, it just says that the array is from that type but the values/object itself isnt saved.

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];

}

And this is the code how i save the objects as XML Files:

        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();

The file is being created and looks like this:

<?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>

And im pretty confident that this array should contain some stuff, but it isnt saved.

If UniverseOut does not have any non-static fields to be serialized, then there's nothing to be saved, as statics are implicitly transient .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM