File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Serilization issue in string object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serilization issue in string object ?" Watch "Serilization issue in string object ?" New topic
Author

Serilization issue in string object ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi All,

I am using serialzation using java but my saved object is in ASCII code, why so ?




In person.txt file is am getting only :
PersonDetails*2#è2namet
’

why so ?
please suggest.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

What output were you expecting?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Your file is not a text file, it's binary. You shouldn't try reading it with a regular text editor. That "ASCII" is merely your editor trying to display binary data.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
It is the text file only i checked it and i am expecting the Object Person state in file with value "prabhat" & 30.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

A file is not a text file by its extension (.txt), but by its content. A quick test could be to open it in an editor like Notepad/TextPad and see if all the chracters are readable.

Serialization in Java does not print just the values of simple properties of the given object. It has to deal with a (possibly) complex object-graph. In other words, the object you're trying to write to a file may have references to other objects, and they in-turn, may have more such references. Hence, Java writes them on the file in a special way which makes it easy to read back (deserialize) the entire object graph from the file. This 'special' way is obviously not something a text-editor like Notepad can read.

What you want, is probably, a simple PrintStream (instead of an ObjectOutputStream) chained to a FileOutputStream. Using this PrintStream, write the property values one by one on the file.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
file is text file only, i had saved alt + F + A with "person.txt" file type "All Type";

could you please help me out in my code how to do this i mean printStream instead of ObjectOutputStream.

i have tried like this but not helping me out.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You still haven't explained your problem.

If you're expecting serialization to produce some specific output different from what it actually does produce, then your expectation is wrong. And if you're expecting to learn something by examining the result of serializing something, that's probably wrong too. There is nothing useful to be learned from that.

So... can you go back and explain why you are doing this?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Serilization issue in string object ?