如果你不太明白:下面我举一些列子你可以参考下:
public class ObjectDemo
{
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{
Person p1=new Person("小刚",34);
//show_1(p1);
show_2(p1);
}
private static void show_2(Person p) throws FileNotFoundException, IOException, ClassNotFoundException
{
ObjectInputStream obi=new ObjectInputStream(new FileInputStream("avn.txt"));
//Object obj=new Object();
Object obj = obi.readObject();
p=(Person)obj;
System.out.println(p.getName()+"***"+p.getAge());
}
public static void show_1(Person p) throws IOException, IOException
{
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("avn.txt"));
//序列化解决问题一:在对象中实现序列化接口Serializable,这个是针对对象的操作而言的
obs.writeObject(p);
//序列话的问题解决问题方式二:
/* try
{
obs.writeObject(p);
} catch (NotSerializableException e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
}*/
obs.flush();
obs.close();
}
}
|