• Post Reply Bookmark Topic Watch Topic
  • New Topic

problems when trying to write an object in a file (not serializable exception)  RSS feed

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I'm working on a program that requires me to read from a file and write that information in another file. When I try to write in the file, Netbeans reports:

The method I use to write is this one:


And the class Paraula is this one:



This is the method I use to read:


I was investigating about notSerializable Exceptions and I found out that if I write in the class Paraula it writes something in the new file, but just "non-sense" characters like
¬í sr PruebaPractFin1.Paraula–/òeÀ–ÏM I longitud[ lletrest [Cxp ,ur [C°&f°â]„¬ xp Zþÿ


Thanks in advance, I hope you can help me out!
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You either need to read and write to the file using the serialization mechanism (that is, implements Serializable, as you found), or use your own way of writing to a file.

Since you are reading a specific format from a file, I'm going to guess that you should be writing the same format out.

At the moment you are just trying to serialise it, which is not going to give you the format you want.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how can I make it without serialising it?
Should I create a different/new method to write?
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your file text? Then I would use a BufferedReader and BufferedWriter.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a text file, but because it is an assignment I must read the file as if it was made of "Paraula" objects (and write it obviously)
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I think you have to serialize it. I believe all that's needed is this:

Then the class SerializablePlay can be serialized.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it sounds like you are already doing this.

I was investigating about notSerializable Exceptions and I found out that if I write
implements Serializable
in the class Paraula it writes something in the new file, but just "non-sense" characters like
¬í sr PruebaPractFin1.Paraula–/òeÀ–ÏM I longitud[ lletrest [Cxp ,ur [C°&f°â]„¬ xp Zþÿ

Those nonsense characters are the binary format that a serialized file uses. You are probably doing everything fine.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, is there a way to change from a binary format to a text format?

Thanks
 
Carey Brown
Bartender
Posts: 3021
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can change, not the file but the display, by reading the objects back into memory and utilizing the toString() methods that you've created to send a textual representation to the console.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a text file, but because it is an assignment I must read the file as if it was made of "Paraula" objects (and write it obviously)

But it doesn't say it has to be serialized? I don't know what reading the file "as if it was made of 'Paraula' objects" means. If you have a text file, I would think reading it with a BufferedReader object would be the way to go. Similarly, "writing as an object" doesn't make much sense if it's not serialized. But a serialized object will always be in binary format.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so maybe if I explain you the problem in detail you will understand me better:

This project for university consists in making a program to allow a certain company to send different letters to its clients (for example, if it's a new client the company will send a letter saying welcome to our family or whatever and etc.)
So I have a file with the names and addresses and different codes for he clients and another file with the codes of the clients and which letter will be sent to each of them, for example:

0001 welcome
0002 new product info
0003 whatever


0001 #n name #a address
0002 #n name #a address


I have to read the info in this files and then I need to write the letters with the address, the name and the different types of letters (welcome, info, deliver) depending on the client.
Obviously these new files (the letters) will have to be readable, so they can't be in binary format.

One of the requirements in this assignment is to use a class called "Paraula" (which I posted before) to "manipulate" the text files.

I hope that with this you will understand me better

Thanks!
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you just need to read the text files with BufferedReader, create the objects and use them to create a line or lines of text, then use BufferedWriter to write to a text file. No need for Serialized at all.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, gonna try to do it that way!

The problem now is that if I'm told that I have to use an specific method to read the files as if they were made of "Paraula " objects, I shouldn't use BufferedReader or should I? Maybe there's a way to combine both ways, but I don't know how.
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you plan to use Java serialization (via "implements Serializable") then you won't be producing a text file, so using a Reader of any kind would be the wrong thing to do.

On the other hand if you planned to use your own custom-designed serialization process, and it produces text, then using a Reader would make sense.

In other words you should use a Reader if and only if your chosen serialization process produces text. However since you apparently haven't chosen a process yet, it is impossible to suggest how you could implement that process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!