aspose file tools*
The moose likes I/O and Streams and the fly likes java.io.Externalizable help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.io.Externalizable help" Watch "java.io.Externalizable help" New topic
Author

java.io.Externalizable help

Mighty Warthog
Greenhorn

Joined: May 21, 2003
Posts: 2
Hello,
I am having trouble with java.io.Externalizable. Below is my code:

The code compiles and executes with error, but the output is different that what I expected. I expected a new file to be created called "file.txt" with content "(0,0)" but instead the file contains "�� sr Point2DY��p.F xpw ( 0 , 0 )x"
Any help?
[ May 22, 2003: Message edited by: Jim Yingst ]
Mighty Warthog
Greenhorn

Joined: May 21, 2003
Posts: 2
i very much need a solution to this problem...please, anyone?
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Mighty,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy
Thanks!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
O Mighty Warthog:
When using writeExternal(), you control how the instance data is serialized - but other parts of the file are outside your control, governed by the Java Object Serialization Specification. For example, the full class name of the instance being written will appear in the file; this is also mentioned in the API for writeExternal(). I'm not sure exactly what all the other stuff is; the point is, you're probably stuck with it if you use ObjectOutputStream and ObjectInputStream - even with Externalizable. If you're trying to create a nice human-readable text file representation, you'd be better off using a PrintWriter wrapping a FileWriter. Parsing this may be a bit more complicated (consider StringTokenizer or the java.util.regex package) but the file can look much nicer.
Note also that the part you wrote with your writeExternal() method is currently appearing as " ( 0 , 0 )". If you were expecting somcething more like "(0,0)", you should carefully read what writeUTF() and writeChar() actually do. They're not really designed for readability; they (like most DataOutput methods) should really be thought of as ways to format binary data. Which incidentally happens to represent strings and chars - but the point is you're going to see other weird characters in there for various reasons. Again, if you want a nice text represntation, use a PrintWriter.


"I'm not back." - Bill Harding, Twister
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Again, if you want a nice text representation, use a PrintWriter.

Jim, please help me over here. wouldn�t a simple implementation of the toString() method suffice?
And, O Mighty Warthog:
Your readExternal method is also flaw for the same reasons as Jim mentioned.Also, I am not sure of the overall efficiency here
Regards,
Leslie
[ May 25, 2003: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
wouldn�t a simple implementation of the toString() method suffice?
Well, yes. Mostly. We're sort of answering the question at different levels - toString() converts the data into character representation, while a FileWriter converts those chars in memory into bytes in a file. The PrintWriter part serves to implicitly invoke toString() on any non-string objects or primitives you didn't explicitly convert.
E.g. you can do something like

or
Or many other possibilities. And the file format I used was just made up on the spot (well, stolen from Properties files and the like) - substitute whatever you want, as long as you can figure out how to parse it later (another detail I omitted). Didn't mean to imply that PrintWriter is the only way - but it's a common tool, good for fixing various errors that people might otherwise commit (like saying writer.write(10) and expecting to see "10" appear in a file). IMHO, YMMV, caveat lector, this space for rent, etc.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Thanks Jim, this was very useful.
So when are you writng your book?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ssshhhhh! Don't let Map hear, you'll start her campaigning again to make me an author against my will. :roll:
I just don't have any particular topic I feel the urge to write a whole book on. I like the forums here, because I can tackle whatever bite-size piece of a topic is of interest to me, without feeling obligated to provide comprehensive coverage of all the related topics, or organize my various ramblings into some sort of cohesive whole.
[ May 26, 2003: Message edited by: Jim Yingst ]
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Jim,
I think you're taking it a bit too serious. Coming mutually from the self-study school, I prefer an informal approach when reading a book.
I don't know anything about Map lighting fires under you. I just happened to follow some leads with you and, based on your composure, the "book thing" was a just a natural request.
If you do go for it any day, I can see that it will be filled with humor and informal and I would love it.
Hey, how about "The best of javaranch"?
OK, for now I'll settle for the formally informal forum posts by the one and only JRY! Or at least to look forward to any newsletters.
I have done my part
Bow,
Leslie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.Externalizable help