# HashSet und XMLEncoder



## Jakob97 (21. Okt 2015)

Hey 

Ich versuche ein HashSet mit allen darin gespeicherten Objekten mittels java.beans.XMLEncoder zu serialisieren.
Die Variable gameFiles ist ein HashSet<FileDescription> mit etwa 16.000 Elementen.


```
public class FileDescription implements Externalizable {
    private String path;
    private long bytes;
    private long lastModified;
   
    public FileDescription() {}
   
    public FileDescription(File f) throws IOException {
        path = f.getCanonicalPath();
        bytes = f.length();
        lastModified = f.lastModified();
    }
   
    public String getPath() {
        return path;
    }
   
    public long getSize() {
        return bytes;
    }
   
    public long getLastModified() {
        return lastModified;
    }
   
    @Override public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(path);
        out.writeLong(bytes);
        out.writeLong(lastModified);
        out.flush();
    }
   
    @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        path = (String) in.readObject();
        bytes = in.readLong();
        lastModified = in.readLong();
    }
}
```


```
private String getClientFilesList() throws IOException {
        String s;
        try (ByteArrayOutputStream os = new ByteArrayOutputStream(); XMLEncoder enc = new XMLEncoder(os)) {
            enc.writeObject(gameFiles);
            enc.flush();
            os.flush();
            s = new String(os.toByteArray()).replaceAll(System.getProperty("line.separator"), "");
        }
        return s;
    }
```

Die Ausgabe:

```
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_60" class="java.beans.XMLDecoder">
<object class="java.util.HashSet">
  <void method="add">
   <object class="at.lasthoren.FileDescription"/>
  </void>
  <void method="add">
   <object class="at.lasthoren.FileDescription"/>
  </void>
  <void method="add">
   <object class="at.lasthoren.FileDescription"/>
  </void>
  <void method="add">
   <object class="at.lasthoren.FileDescription"/>
  </void>
</object>
```


----------

