wood burning stoves*
The moose likes Beginning Java and the fly likes ObjectOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ObjectOutputStream" Watch "ObjectOutputStream" New topic
Author

ObjectOutputStream

Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Hi!
I 'm trying to use an ObjectOutputStream object to output some objects to a file named "AfterTrans.init". The code below is the one I am using. For some reason, IO exception is being caught. By the way, 'Account' is a class defined by myself and 'AccountList' is a vector of 'Account' objects. Also, the class 'Account' implements Serializable and I have imported the class java.io.Serializable. Is the problem arising because I am NOT creating a 'Account' object using 'new' operator?! If thatz the case, doesn't make any sense to me because "re"-creating the existing objects just to output them to a file is so very redundant!

Any help will be appreciated! Thanks!!
-K
(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited August 07, 2001).]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

You don't need to construct explicitly just to get Object Serialization.
You might call printStackTrace() in your catch code to see if you can get any more useful information. I'm not seeing anything obvious.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Thank You Michael for the reply. I did figure out the problem using printStackTrace(). Actually, the 'Account' class had, as one of its data members, an object of a different class 'ProductMatrix'. I didn't know even that class i.e. the 'Product Matrix' class needs implement the interface Serializable for the object of 'Account' class to be serialized properly.
But now, I am having some other problem with the code. Now no exception is thrown; but instead of the details of the 'Account' class objects, some junk (absolute junk rather!) is being written to the file "AfterTrans.init". Is there some other condition I need check in order that the objects are 'serialized properly'?! Is the code missing something very basic??
Thanks!
-K
Larry Anderson
Greenhorn

Joined: Aug 06, 2001
Posts: 26
Any example as to what's being written to the file? Is that junk serialized data that simply looks like junk, but when read in and casted to an object becomes that object again?
�� ur [[[Ljava.lang.String;s>n�j��- xp ur [[Ljava.lang.String;2M��2�W

--That's an example of some of the stuff in one of my serialized object files, not a lot of useful info, but very good for storing a Java object. Try reading in the java object. Sorry if this doesn't help, but there's not a lot you can do without an example.
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Thanks Larry for the reply!
Exactly!!! Thatz what the junk is looking like! I didn't try reading back the 'junk' into an object...I will try it. But that will not be solving my problem because the requirement is that the output of the program should be a list of accounts in a 'readable' format, in a physical file.
Thanks once again for the info.
-K
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Suma, That "junk" is your serialized object. It sounds like you want actual text data in your file. If this is the case, you should not use object serialization for this. You should use a Writer to write data as text to the file.
Chad
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Thanks Chad! That is what even i see........We can't use ObjectOutputStream alone (i.e. without chaining it to another stream object) in order to get a "textual" output, i guess...
-K
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Yes, but the question is: Do you need to store this data as objects to be used again? Or do you only need to write this data to a text file?
Chad
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
I just need store the objects in a file and will not be reading back the objects. Anyway, I could get the thing done using the following code:
------------------------------------------------------------------
FileWriter f = new FileWriter("AfterTrans.init",true);
BufferedWriter out = new BufferedWriter(f);
//File f = new File("AfterTrans.init");
//ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(f));
Account accobj;
String s = "";
for (int sz = 0; sz < AccountList.size(); sz++)
{
accobj = (Account)(AccountList.elementAt(sz));
s = s + accobj.toString() + "\n";
//output.writeObject(accobj);
}
out.write(s,0,s.length());
out.close();
out.flush();
---------------------------------------------------------------
As such, I did know that a Writer object or one of its sub-class' objects would be able to do this; but wanted to use ObjectOutputStream just to get out of the mind-block; but ended up 'strengthening' that block!! :-)
Thanks for the help anyway!
-K
 
Don't get me started about those stupid light bulbs.
 
subject: ObjectOutputStream