Re: writeObject IOException
javafan wrote:
As I know String and char are serializable and these are the only fields in
my class:
public class Person implements Serializable {
private String id;
private String unit;
private char[] code;
...//methods
Your program is not doing what you expect. Therefore it seems likely
that at least one thing you know to be true about it is wrong. For
instance, perhaps you have an inner class which will attempt to
serialise the outer instance.
There are lots of fun things with ObjectOutputStream. For instance the
code below prints the class of objects that are written to the stream.
In the case below you see the object we are interested in, its simple
field and then the outer instance (followed by an exception).
Tom Hawtin
import java.io.*;
class Write {
public class SimpleThing implements Serializable {
private final String simpleField = "My simple field";
}
public static void main(String[] args) throws Exception {
new Write().write();
}
private void write() throws Exception {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut) {
{
enableReplaceObject(true);
}
@Override
protected Object replaceObject(
Object obj
) throws IOException {
System.err.println(
obj==null ? null : obj.getClass()
);
return super.replaceObject(obj);
}
};
out.writeObject(new SimpleThing());
}
}
--
Unemployed English Java programmer
http://jroller.com/page/tackline/