inifiles with serialize ??

  • Thread starter =?iso-8859-1?q?J=FCrgen_Gerstacker?=
  • Start date
?

=?iso-8859-1?q?J=FCrgen_Gerstacker?=

Hello,
I need to store some variables on harddisk in order to restore their
values in successive sessions.

I took this simple aproach:

Inifile as map, loaded at the startup.
- - - - - - - - - - - - - - - - - - - -
Map<String, String> ini=new HashMap<String,String>();
try {
ObjectInputStream input=new ObjectInputStream(new
FileInputStream(inifile));
ini=(HashMap<String,String>)input.readObject();
} catch (IOException e) {}
catch (ClassNotFoundException e) {}
- - - - - - - - - - - - - - - - - - - -

Changes during the session
ini.put("key","value");


Storing at closing app:
- - - - - - - - - - - - - - - - - - - -
public void windowClosing(WindowEvent e) {
try {
ObjectOutputStream output=new ObjectOutputStream(
new FileOutputStream(inifile));
output.writeObject(ini);
} catch (IOException ex) {}
- - - - - - - - - - - - - - - - - - - -


My question is:
How realible is this approach with respect to future changes of Java?
The byte-representation of Map might change ?

Juergen
 
T

Tom Hawtin

Jürgen Gerstacker said:
How realible is this approach with respect to future changes of Java?
The byte-representation of Map might change ?

It's the serial representation of the run-time class that matters. In
this case, HashMap (and String which is treated specially by
Object(In|Out)putStream). Fortunately HashMap is designed to have a a
serial representation that isn't a hash map - it's actually a series of
keys and values. So even as the algorithm that HashMap uses changes,
there is serial compatibility (for instance, during 1.4 it went from
prime table sizes to powers of two, and also applied a rehashing
function to hash codes).

As you get away from the core classes, attention to serialisation wanes.
Swing notably is not compatible between version. Indeed in untrusted
code 1.5 couldn't even deserialise for a year or so. java.beans has a
form of serialisation based on public getters and setters designed for
Swing, but personally I wouldn't trust that either.

To reliably serialise your own classes takes some effort. I believe
Effective Java covers some of the isseus:

http://www.amazon.co.uk/dp/0201310058/

New "Effective Java Programming Language Guide" out 29 Jun 2007
according to Amazon UK.

http://www.amazon.co.uk/dp/0321356683/

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top