Java Reference
In-Depth Information
FileOutputStream(filename);
try (ObjectOutputStream oos = new
ObjectOutputStream(fos)) {
oos.writeObject(settings);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private ProgramSettings loadSettings(String filename)
{
try {
FileInputStream fis = new
FileInputStream(filename);
ObjectInputStream ois = new
ObjectInputStream(fis);
return (ProgramSettings) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
How It Works
Java supports serialization , which is the capability of taking an object and creating a
byte representation that can be used to restore the object at a later time. By using an in-
ternal serialization mechanism, most of the setup to serialize objects is taken care of.
Java will transform the properties of an object into a byte stream, which can then be
saved to a file or transmitted over the wire.
Note The original Java Serialization framework uses reflection to serialize the ob-
jects, so it might be an issue if serializing/deserializing heavily. There are plenty of open
source frameworks that offer different trade-offs depending on your need (speed versus
Search WWH ::




Custom Search