Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

having trouble writing/reading objects to a file - bad serialization ?

Status
Not open for further replies.

vesper8

Programmer
Jul 21, 2006
1
0
0
CA
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

Code:
public class Occupant implements 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

Code:
  public void 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);
    }
  }

  public void 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!
 
// this works just fine, may be useful for your purpose
import java.io.*;

public class MyObject implements Serializable{
private String name;
private double price;

MyObject(String n,double p){
name=n;
price=p;
}

public String toString(){
return name + "\t" + price;
}

public static void main(String[]args){
try{
File f=new File("myobjects.dat");
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois=new ObjectInputStream(fis);
//FileOutputStream fos=new FileOutputStream(f);
//ObjectOutputStream oos=new ObjectOutputStream(fos);

/**
MyObject[] mo={new MyObject("House",123.99),new MyObject("Car",12.99),new MyObject("Paper",3.78)};
for(int j=0;j<3;j++)
oos.writeObject(mo[j]);
oos.close();
System.out.println("Objects written to file!");
*/

for(int j=0;j<3;j++){
MyObject o=(MyObject)ois.readObject();

System.out.println(o.toString());

}

ois.close();
}catch(Exception ex){ ex.printStackTrace(); }
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top