having trouble writing objects to file and reading it back - serialization

Hello all. I'm trying to read/write objects to a file using streams.

Basically I have a table that contains up to 10 objects. Each object contains several pieces of data such as a name and adress etc

both my class and the object's class are implementing java.io.Serializable

here's the top of my object's class

publicclass Occupantimplements java.io.Serializable

{

String occupantName, adress;

public Occupant( String n, String a )

{

this.playerName = n;

this.adress = a;

}

and here are my filewriter and filereader functions in my other class that creates a table[10] and adds 10 occupants to it

publicvoid writeFile()

{

try

{

FileOutputStream out =new FileOutputStream("datafile.data");

ObjectOutputStream oos =new ObjectOutputStream(out);

for (int i = 0 ; i < 10; i++)

{

oos.writeObject ( Occupants[i] );

}

oos.flush();

}

catch (Exception e)

{

System.out.println("Problem serializing: " + e);

}

}

publicvoid readFile()

{

try

{

FileInputStream in =new FileInputStream("datafile.data");

ObjectInputStream ois =new ObjectInputStream(in);

Occupant Occupants = (Occupant)ois.readObject();

}

catch (Exception e)

{

System.out.println("Problem serializing: " + e);

}

}

and here's my problem. When I write to the file.. If I look at the file's contents afterwards it shows that it only wrote the first piece of data for each object. In this case only the name but not the adress.

And if I try to load that back into a table of objects well that just plain don't work.. I'm obviously missing something pretty important.. but I've been reading on serialization and I tried a couple of things but still haven't found what I'm doing so wrong.

I'd really appreciate if someone could point out to me the proper way of doing what I want to do which is writing and reading objects with complex structures that contain lots of data

thanks a lot!

Message was edited by:

vesper8

Message was edited by:

vesper8

null

[3389 byte] By [vesper8a] at [2007-10-3 1:04:55]
# 1
> ... occupantName ...> ... this.playerName = ...That's a typo, no?What about explicitly closing the FileIn/OutputStream after having used it?
quittea at 2007-7-14 18:01:21 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm not sure what your mistake is, but I found several other flaws in your code. First the unimportant one:

1) don't hide Try/Catch! Exceptions are meant to give either the program or the user a chance to fix the problem. They are NOT meant for debugging! Let your save/load methods throw the Exception, so that you can catch it where you want.

2) Your load method doesn't have a return value. You should return what you have loaded.

3) You write several objects, but you only load one. You have to loop through the whole ObjectInputStream, to get all objects, and store them in an array.

Since the ObjectStream is binary, you have no chance to read the saved file in notepad. That you can read anything, is pure coincidence. It's like looking for plain text in an exe.

Mongera at 2007-7-14 18:01:21 > top of Java-index,Java Essentials,Java Programming...