Help with Serializable

S

stevieweasle

Hi
I can't work out why my writeObject and readObject methods are not
working with the Serializable interface. I want to be able to write
private fields to my output stream without using get and set methods.
I've tried with and without defaultWriteObject and defaultReadObject
methods, and also with and without the transient keyword. "testString"
does not get written to the xml file and I don't think either
writeObject or readObject are getting called during Serialization. No
errors are generated and if I add getTestString and setTestString
methods for testString then it starts working.

Here is the code, I'm using J2SE 1.4.2.

I'd really appeciate any help.

Thanks

TestSerialization.Java
----------------------
package Test;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TestSerialization {

public static void saveObject() {
Test test = new Test();
test.setTest("Test to use writeObject and readObject");

FileOutputStream fos = null;
try {
fos = new FileOutputStream("test.xml");
XMLEncoder oos = new XMLEncoder(fos);
oos.writeObject(test);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void readObject() {

try {
FileInputStream fis = new FileInputStream("test.xml");
XMLDecoder ois = new XMLDecoder(fis);
Test test = (Test) ois.readObject();
ois.close();

System.out.print(test);

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
saveObject();
readObject();
}
}


Test.java
---------
package Test;

import java.io.Serializable;
import java.io.IOException;

public class Test implements Serializable {
private transient String testString;

public Test() {
testString = "";
}

public void setTest(String testString) {
this.testString = testString;
}

private void writeObject(java.io_ObjectOutputStream out) throws
IOException {
out.defaultWriteObject();
out.writeObject(testString);
}

private void readObject(java.io_ObjectInputStream in) throws
IOException, ClassNotFoundException {
in.defaultReadObject();
this.testString = (String) in.readObject();
}

public String toString() {
return testString;
}
}
 
S

Sudsy

Hi
I can't work out why my writeObject and readObject methods are not
working with the Serializable interface. I want to be able to write
private fields to my output stream without using get and set methods.
I've tried with and without defaultWriteObject and defaultReadObject
methods, and also with and without the transient keyword. "testString"
does not get written to the xml file and I don't think either
writeObject or readObject are getting called during Serialization. No
errors are generated and if I add getTestString and setTestString
methods for testString then it starts working.

From the javadocs for XMLEncoder:

"Despite the similarity of their APIs, the XMLEncoder class is
exclusively designed for the purpose of archiving graphs of JavaBeans as
textual representations of their public properties."

That's why people suggest that you RTFM. Two important points here:
- JavaBeans means classes which adhere to the standard, which mandates
public accessor and mutator methods
- "public properties": it says so right there!

You'll have to reconsider your approach. Why you want to store your
objects as XML is quite beyond me...
 
S

stevieweasle

Thanks Sudsy, I didn't fully understand that when I read the
XMLEncoder API.

The main reason I am using XMLEncoder is that "documents written this
way have a natural immunity to changes in the implementations of the
classes involved". I've found this useful because my classes are
evolving and they are resilient in that if a problem occurs it can
bypass the error and not invalidate the serialization completely.

If you or anyone can suggest some alternatives I would be grateful,
I've considered using a database but XML Serialization seemed the
perfect answer.

Steve
 
S

Sudsy

Thanks Sudsy, I didn't fully understand that when I read the
XMLEncoder API.

The main reason I am using XMLEncoder is that "documents written this
way have a natural immunity to changes in the implementations of the
classes involved". I've found this useful because my classes are
evolving and they are resilient in that if a problem occurs it can
bypass the error and not invalidate the serialization completely.

If you or anyone can suggest some alternatives I would be grateful,
I've considered using a database but XML Serialization seemed the
perfect answer.

Steve

There has been some discussion of this issue on this newsgroup of
late. I'd suggest you go straight to the horse's mouth:
<http://java.sun.com/j2se/1.3/docs/guide/serialization/spec/class.doc6.html#4100>
 
S

stevieweasle

This is a good article on serialization.

But the way my classes will develop I'll be breaking all the interface
rules that must be kept for Serialization to work.

What I need is a simple way to write my classes to file in a readable
format; i.e. in either structured text or a file database format like
the old paradox.

If I'm going to use a database format I need to be able to deploy the
application easily without any third party software installations.

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,293
Latest member
BonnieHamb

Latest Threads

Top