- Java code
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){}}
------解决方案--------------------
你序列化的时候d:\\test.person 你拼错了,还有一些问题,给你贴出代码给你看下
- Java code
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; }}