File I/O JAVA JDK 1.3 question - Urgent

Mule

Golden Member
Aug 9, 2000
1,207
0
0
I have two Class Objects that implement serializable and I need to be able to read and write them to a single file. The Objects are tutordb and admindb. I want do do this using ObjectOutputStream, ObjectInputStream, FileInputStream, and FileOutputStream.

I have never done this before so could someone help me out here? Or link me to a place where I can look at some examples? This is what i have come up with, i don't know if it is right.

FileOutputStream ostream = new FileOutputStream("cstutor.cst");
ObjectOutputStream p = new ObjectOutputStream(ostream);

p.writeObject(Object obj);
p.writeObject(Object obj);

p.flush();
ostream.close();

When the arguments are Object obj do I have to provide an instance of that object or the object itself?

Actually admindb and tutordb are packages, so do I have to write every Object in that package to ObjectOutputStream or just the top level Object? Top level Objects would be UserDB for admindb and TutorialDB for tutordb.

Someone please show me how this works!


 

javathehut

Senior member
Oct 23, 2000
318
0
0
go to http://java.sun.com/products/jdk/1.2/docs/api/java/io/ObjectOutputStream.html for the documentation for the ObjectOutputStream. I think that you need to create a File Object first before you do an ObjectOutputStream.
File file = new File("index.data");
try
{
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(Object); // serialize object to file
out.close();
}
catch (Exception e)
{}

The trick is to make the object class implement java.io.Serializable. Then you have to override certain methods. You should have no problems then. Good luck!
 

Mule

Golden Member
Aug 9, 2000
1,207
0
0
I think I have gotten it down, except for


<< out.writeObject(Object); // serialize object to file >>


Would I just do : out.writeObject(UserDB); //to serialize object UserDB to file?

And also as I stated before I want to take the contents of the whole package and serialize to a file, would I have to serialize each Object within the package or just the Top Level Object?
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
Would I just do : out.writeObject(UserDB); //to serialize object UserDB to file?

UserDB is your Class but you woud want to use the instance of that class. I asumed you have done something like the following:

UserDB userdb = new UserDB();

Therefore, you would probably want to do the following:

out.writeObject( (UserDB) userdb );
 

javathehut

Senior member
Oct 23, 2000
318
0
0
userDB should be an instance of a class that implements serializable. I think that you have to serialize each object in order to write it, unless you serialize a parent class then all the children would also be serializable.
 

Mule

Golden Member
Aug 9, 2000
1,207
0
0


<< out.writeObject( (UserDB) userdb ); >>


Why would I need to put UserDB in parenthesis? Isn't that used only to cast type something? I thought it would be: out.writeObject(UserDB userdb);

or am i wrong again?
 

Mule

Golden Member
Aug 9, 2000
1,207
0
0
Thanks guys for helping me, transition from C++ to JAVA is a PAIN!! :p