File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Serilization issue in string object ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 397
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 :

why so ?
please suggest.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4594

What output were you expecting?

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

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.

How To Ask Questions How To Answer Questions
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 397
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: 397
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

Joined: Oct 14, 2005
Posts: 19973

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?
I agree. Here's the link:
subject: Serilization issue in string object ?
It's not a secret anymore!