Serialization

following is an excerpt from our very own Kathy. its not exactly the way it is in her book. my que is in the code itself. its commented .

import java.io.*;

class SuperNotSerial

{

public static void main(String[] ar)

{

Dog d=new Dog(100,"fido");

Dog d2=null;

System.out.println("before: "+d.name+" "+d.weight);

try

{

FileOutputStream fs=new FileOutputStream("test.txt");

ObjectOutputStream os=new ObjectOutputStream(fs);

os.writeObject(d);

os.close();

}catch(Exception e){}

try

{

FileInputStream fis=new FileInputStream("text.txt");

ObjectInputStream ois=new ObjectInputStream(fis);

d2=(Dog)ois.readObject();

System.out.println("after2: "+d2.name+" "+d2.weight);//this line never gets printed.y?

ois.close();

}catch(Exception e){}

System.out.println("after: "+d2.name+" "+d2.weight);

}

}

class Dog extends Animal implements Serializable

{

String name;

Dog(int w,String name)

{

this.name=name;

this.weight=w;

}

}

class Animal

{

int weight=45;

}

[1183 byte] By [sjosh_theonea] at [2007-11-27 11:25:36]
# 1

Please format your code.

http://forum.java.sun.com/help.jspa?sec=formatting

~

yawmarka at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...
# 2

We shouldn't have to pick through your code to track down the error, let alone the actual question. Make our job as simple as possible, please

georgemca at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...
# 3

Don't do this:

catch(Exception e){}

At the very least, print the stack trace. The point of exceptions is that they tell you when something exceptional happens, like say trying to serialize something that can't be serialized, or opening a non-existent file.

catch(Exception e){

e.printStackTrace();

}

hunter9000a at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...
# 4

following is an excerpt from our very own Kathy. its not exactly the way it is in her book. my que is in the code itself. its commented .

import java.io.*;

class SuperNotSerial

{

public static void main(String[] ar)

{

Dog d=new Dog(100,"fido");

Dog d2=null;

System.out.println("before: "+d.name+" "+d.weight);

try

{

FileOutputStream fs=new FileOutputStream("test.txt");

ObjectOutputStream os=new ObjectOutputStream(fs);

os.writeObject(d);

os.close();

}catch(Exception e){}

try

{

FileInputStream fis=new FileInputStream("text.txt");

ObjectInputStream ois=new ObjectInputStream(fis);

d2=(Dog)ois.readObject();

System.out.println("after2: "+d2.name+" "+d2.weight);//this line never gets printed.y?

ois.close();

}catch(Exception e){}

System.out.println("after: "+d2.name+" "+d2.weight);

}

}

class Dog extends Animal implements Serializable

{

String name;

Dog(int w,String name)

{

this.name=name;

this.weight=w;

}

}

class Animal

{

int weight=45;

}

HAPPY

sjosh_theonea at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...
# 5

n on top of all the weirdness superclass constructor is never called!!!

sjosh_theonea at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...
# 6

this is because an exception is being thrown and you are swallowing it @

catch(Exception e){}

replace it with

catch(Exception e){

e.printStackTrace(System.out);

}

kilyasa at 2007-7-29 16:05:24 > top of Java-index,Java Essentials,Java Programming...