Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
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

Win a copy of The Software Craftsman this week in the Agile forum!
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: 3680

What output were you expecting?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19958

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: 19359

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?
Have you tried LearnNowOnline?
subject: Serilization issue in string object ?