2 ms·
One of my favorite things to do in java. Save anything to XML, it even handles all the objects your objects are pointing to. All you need to do is make your obj
by hmottestad 15y ago
One of my favorite things to do in java. Save anything to XML, it even handles all the objects your objects are pointing to. All you need to do is make your objects serializable. Then java handles everything else using reflection.
public void saveAll(Object[] objects) {
File file = new File(filename + ".xml");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
XMLEncoder xenc = new XMLEncoder(bos);
for (Object o : objects) {
xenc.writeObject(o);
}
xenc.close();
} catch (FileNotFoundException e) {
System.err.println("File not found");
} catch (IOException e) {
System.err.println("Some other error");
}
}