/* 实现串行化对象*/
public class Student implements Serializable{
int id; //学号
String name; //姓名
int age; //年龄
String department; //系别
public Student(int id,String name,int age,String department){
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
}
/*读写上边的串行化对象*/
/**
* 串行化对象保存
* @param ob 要保存的对象
* @param path 保存对象的路径 保存串化行对象的文件 注意后缀.ser
*/
public static void saveSerob(Object ob,String path){
try {
FileOutputStream fo=new FileOutputStream(path); //保存对象的状态
ObjectOutputStream so=new ObjectOutputStream(fo);
so.writeObject(ob);
so.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 恢复串行化的对象
* @param path 保存对象的路径 保存串化行对象的文件 注意后缀.ser
* @return Object
*/
public static Object readSerob(String path){
Object ob=null;
try {
FileInputStream fi=new FileInputStream(path);
ObjectInputStream si=new ObjectInputStream(fi); //恢复对象的状态
ob= si.readObject();
si.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ob;
}