• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Serilization issue in string object ?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 14094
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What output were you expecting?
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 27287
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic