Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

writeObject writes rubbish  RSS feed

 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dominique Ramoney
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!