package test;
import java.io.*;public class SerializableTest { public static void main(String[] args){

/*
 * 
 * 序列化
 */
FileOutputStream fos  = null;
ObjectOutputStream oos = null;
Person person = new Person("小明",32);
person.setSpouse("春花");
 
try {

fos = new FileOutputStream("d://text.person");
oos = new ObjectOutputStream(fos);
oos.writeObject(person);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}

/*
 * 
 * 反序列化
 */
 Person p = null;  
try {
FileInputStream fis = new FileInputStream("d://test.person");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

if(obj instanceof Person)
/*
 *  //这里的变量p为什么要在上面Person p = null定义,
 *  直接在这里Person p = (Person)obj要报错,是什么原因啊???
 */
 p  =  (Person)obj;  

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}} 
/*
 * 可序列化的类
 */
class Person implements java.io.Serializable{


/**
 * 
 */
private static final long serialVersionUID = 1L;

private String name; 
private int age;
private String spouse;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}


public Person(){}

public Person(String name,int age){}
}

解决方案 »

  1.   

    你序列化的时候d:\\test.person 你拼错了,还有一些问题,给你贴出代码给你看下import java.io.*;public class SerializableTest {    public static void main(String[] args){        /*
            *
            * 序列化
            */
            FileOutputStream fos  = null;
            ObjectOutputStream oos = null;
            Person person = new Person("小明",32);
            person.setSpouse("春花");        try {            fos = new FileOutputStream("d://test.person");
                oos = new ObjectOutputStream(fos);
                oos.writeObject(person);
                oos.close();
                fos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{        }        /*
            *
            * 反序列化
            */
            Person p = null;
            try {
                FileInputStream fis = new FileInputStream("d://test.person");
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object obj = ois.readObject();            if(obj instanceof Person)
                    /*
                     *  //这里的变量p为什么要在上面Person p = null定义,
                     *   Person p = null根本无所谓
                     *  直接在这里Person p = (Person)obj要报错,是什么原因啊???
                     *   直接这样转换也不会报错
                     */
                    p  =  (Person)obj;
                    System.out.println(p);        } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    }}
    /*
    * 可序列化的类
    */
    class Person implements java.io.Serializable{
        /**
         *
         */
        private static final long serialVersionUID = 1L;    private String name;
        private int age;
        private String spouse;    @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", spouse='" + spouse + '\'' +
                    '}';
        }    public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSpouse() {
            return spouse;
        }
        public void setSpouse(String spouse) {
            this.spouse = spouse;
        }
        public Person(){}    public Person(String name,int age){
            this.name=name;
            this.age=age;
        }
    }
      

  2.   


    package test;
    import java.io.*;public class SerializableTest {    public static void main(String[] args){
            
            /*
             * 
             * 序列化
             */
            FileOutputStream fos  = null;
            ObjectOutputStream oos = null;
            Person person = new Person("小明",32);
            person.setSpouse("春花");
             
            try {
                
                fos = new FileOutputStream("d:\\text.person");
                oos = new ObjectOutputStream(fos);
                oos.writeObject(person);
                oos.close();
                fos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                
            }
            
            /*
             * 
             * 反序列化
             */
               
            try {
                FileInputStream fis = new FileInputStream("d:\\text.person");
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object obj = ois.readObject();
                
                Person p =(Person) obj;
                    /*
                     *  //这里的变量p为什么要在上面Person p = null定义,
                     *  直接在这里Person p = (Person)obj要报错,是什么原因啊???
                     */
                System.out.println(p.getSpouse());
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }} 
    /*
     * 可序列化的类
     */
    class Person implements java.io.Serializable{
        
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        private String name; 
        private int age;
        private String spouse;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSpouse() {
            return spouse;
        }
        public void setSpouse(String spouse) {
            this.spouse = spouse;
        }
        
        
        public Person(){}
        
        public Person(String name,int age){}
    }
    除了路径拼写错误,没有什么错误呀,楼主说得我也试了,没问题呀
      

  3.   

    看错误信息就能看出来了,
    FileInputStream fis = new FileInputStream("d:\\text.person");
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object obj = ois.readObject();由于路径写错,所以你的obj根本就读不出个东西来,所以你在转换的时候,也会报错。