Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Append writeObject()  RSS feed

 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a problem when trying to write objects to a file .....I have populated a list with objects, than I traverse it, and each time i'm using writeObject() to try to write to the specified file. When I dont add the boolean value true after the filename, the objects are written to file but of course overwritten each time, but when i add true after the filename, the program either writes the objects one after the other without creating a new line OR ELSE writes chinese like characters....
could it be the ObjectOutputStream is picking something along the line ???

Thanks for any input

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialized objects are stored in binary format, not text. Those Chinese like characters are just what your text editor makes of "characters" it doesn't understand well, because they aren't actually characters but bytes.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so therefore i should not use serialized objects ?? or is there a way to convert them to text ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:so therefore i should not use serialized objects ??


That depends. If you just want to transfer objects from one place to another, then sure, use that mechanism; that's what it's for, after all.

On the other hand, if you want humans to be able to look at the contents of the serialized objects while they are in their serialized state, then no, that approach won't work.

or is there a way to convert them to text ?


Depending on what your actual goals and requirements are, something like java.beans.XMLEncoder and XMLDecoder might work for you.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Marco Galea wrote:
On the other hand, if you want humans to be able to look at the contents of the serialized objects while they are in their serialized state, then no, that approach won't work.

or is there a way to convert them to text ?


Depending on what your actual goals and requirements are, something like java.beans.XMLEncoder and XMLDecoder might work for you.


I understand what you're saying, but the thing that is actually confusing me is that sometimes it does write to the file in a human readable format (which is ultimately my goal)...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:
Jeff Verdegan wrote:
Marco Galea wrote:
On the other hand, if you want humans to be able to look at the contents of the serialized objects while they are in their serialized state, then no, that approach won't work.


or is there a way to convert them to text ?


Depending on what your actual goals and requirements are, something like java.beans.XMLEncoder and XMLDecoder might work for you.


I understand what you're saying, but the thing that is actually confusing me is that it sometimes it does write to the file in a human readable format (which is ultimately my goal)...


If ObjectOutputStream happens to leave you with something you can read, that's just coincidence, a special case. For that particular object, all the bytes it wrote out happened to correspond to readable characters or whitespace. Just because it's binary, that doesn't mean it won't include--or even consist mainly of--human readable characters.

Bottom line, it's not intended for human consumption, and you can't rely on being able to read it, nor should you use it if you want humans to be able to read it.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont want to sound like a hard headed guy or something, but the thing is that when i say in readable human format i'm actually implying that it has the details exactly as i've inputted them and not just random characters which i can understand.
And if ObjectOutputStream outputs binary only, than why ONLY when i add the boolean true after the filename (which is supposed to append) the data gets "corrupted" ???

Thanks for your help
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:I dont want to sound like a hard headed guy or something, but the thing is that when i say in readable human format i'm actually implying that it has the details exactly as i've inputted them and not just random characters which i can understand.


SSCCE or it didn't happen.

And if ObjectOutputStream outputs binary only


Nobody ever said that. And remember, even text files are binary. Everything on your computer is binary. It just so happens that "text" is a subset of binary.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Marco Galea wrote:I dont want to sound like a hard headed guy or something, but the thing is that when i say in readable human format i'm actually implying that it has the details exactly as i've inputted them and not just random characters which i can understand.


SSCCE or it didn't happen.


For example, if you run the following (changing the file path appropriately for your system), I promise, you will NOT see simply "abc" in String.out or "123" in Integer.out.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi there.... it's happening again even with this code. If you don't get the output correct in test.txt the first time try to change something from the code (example: delete a curly brackets and then put them back again) an re-compile and u'll get correct output after...... at least that's what is happening to me... :S
Thanks again for your interest
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:when i say in readable human format i'm actually implying that it has the details exactly as i've inputted them and not just random characters which i can understand.

The file does have the data exactly as you wrote it, and there's no problem reading it through an ObjectInputStream. You seem to assume that the file should also be human-comprehensible, but that's something that binary serialization simply doesn't do.

Bottom line: if you want something that's reliably human-readable, don't use binary serialization; use XML serialization via the XMLEncoder/XMLDecoder classes Jeff mentioned. Note that those require your objects to follow JavaBeans conventions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:[code=java]import java.io.*;
Hi there.... it's happening again even with this code. If you don't get the output correct in test.txt the first time try to change something from the code (example: delete a curly brackets and then put them back again) an re-compile and u'll get correct output after...... at least that's what is happening to me... :S
Thanks again for your interest


No, that use of ObjectOutputStream does not produce a human-readable text file. Not for you or me or anybody. You cannot open that flie with notepad or vi or whatever you're using to edit Java code and read it. You may see some pieces you recognize--class names and String member variables--but it's a binary file, with lots of non-human-readable stuff.

Randomly deleting and re-adding braces does not alter the behavior, I guarantee it.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Marco Galea wrote:[code=java]import java.io.*;
Hi there.... it's happening again even with this code. If you don't get the output correct in test.txt the first time try to change something from the code (example: delete a curly brackets and then put them back again) an re-compile and u'll get correct output after...... at least that's what is happening to me... :S
Thanks again for your interest


No, that use of ObjectOutputStream does not produce a human-readable text file. Not for you or me or anybody. You cannot open that flie with notepad or vi or whatever you're using to edit Java code and read it. You may see some pieces you recognize--class names and String member variables--but it's a binary file, with lots of non-human-readable stuff.

Randomly deleting and re-adding braces does not alter the behavior, I guarantee it.


you're just right, now i understood what was exactly happening in the first place.... Thank you very much for your input
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! I'm glad it's clear now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!