Forums Register Login

writeObject writes rubbish

+Pie Number of slices to send: Send
Hi there

I'm having trouble using the writeObject method. Using the example at java.sun.com "How to Write to an ObjectOutputStream", my resultant file contains the following;

��tTodaysrjava.util.DatehjKYtxp�P��:x

and the example "How to Read from an ObjectInputStream" cannot read the file.


Any help would be most appreciated.

Rgds
DR
+Pie Number of slices to send: Send
Hi Dominique,

Welcome to JavaRanch!

The file contains binary data, and it's supposed to look like rubbish, so up to that point, nothing's wrong. What happens, exactly, when you try to read the file back in?
+Pie Number of slices to send: Send
Thanks, after some further reading I did find that the file is supposed to look that and I'm supposed to be able to read it back using readObject but when I try to use readObject, I get nothing.

Here's the test code.

Write;
FileOutputStream out = new FileOutputStream("theTime");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject("Today");
s.writeObject(new Date());
s.flush()

read;
FileInputStream in = new FileInputStream("theTime");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();

My objective is to be able to instantiate an object and then write the details of that object to a file. I'm trying to use the TreeMap class to store objects. And then I want to be able to modify the object values and write them to a file. I think I'm going around in circles at the moment. I was trying to do the writing to file without using get() because I don't want to write it in the format I'm using in the objects toString method. Basically what I really need to do (and it's getting blurrier by the minute) is extract individual object values from the objects in my container class. Forgive me if I sound insane.

+Pie Number of slices to send: Send
Here's your code, very slightly tweaked and made into a complete program:


If I compile and run this, I get



Does this work for you, too? It seems that the code you've written here is just fine.

Now, the other stuff you were writing -- I wouldn't say you sound insane, but perhaps somewhat confused. If you want to talk about your design, we're all ears.
+Pie Number of slices to send: Send
Thanks a million. I'll go from there, I think I'm starting to get the picture. I'll digest this for a while and get back to you when I hit the wall again.

Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1002 times.
Similar Threads
db file
ObjectOutputStream??
transient variable ? is there any other way
EOF in FileInputStreams
How to delete an object which is seraliazed from the file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:02:01.