• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serilization issue in string object ?

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output were you expecting?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20171
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic