三、使用序列化与反序列化对对象进行存储及复原
1、使用File文件工具类进行文件指定类型文件筛选
2、使用io流对文件进行copy
3、使用序列化与反序列化对对象进行存储及复原
- 第一步,创建一个实体类 Student,切记要实现 Serializable 接口,否则无法进行序列化存储!
public class Student implements Serializable {
static final long serialVersionUID = 1L;
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 第二步,创建存储方法,且将存储文件和存储对象以参数方式传入
/**
* 3.1、使用序列化对对象进行存储
* @param file 对象存储文件
* @param o 对象
*/
public void fileStoreBySerialize(File file, Object o){
...
}
- 第三步,判断文件是否存在,且是否为文件夹,若文件为空或为文件夹都打印输出语句;
// 判断文件是否存在
if (file.exists()) {
// 如果存在
// 判断是否是文件夹
if (!file.isDirectory()) {
// 如果是文件
// 对对象进行存储
...
}
} else {
// 如果是文件夹
System.out.println("该文件为文件夹,无法存储对象信息");
}
} else {
// 如果不存在
System.out.println("文件不存在!");
}
- 第四步,使用 ObjectOutputStream() 对象输出流对对象进行存储;
// 判断文件是否存在
if (file.exists()) {
// 如果存在
// 判断是否是文件夹
if (!file.isDirectory()) {
// 如果是文件
// 对对象进行存储
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(o);
System.out.println(o.toString());
System.out.println("对象存储成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// 如果是文件夹
System.out.println("该文件为文件夹,无法存储对象信息");
}
} else {
// 如果不存在
System.out.println("文件不存在!");
}
- 第五部,创建复原方法,将所需复原文件以参数形式传入;
/**
* 3.2、使用反序列化对对象进行复原
* @param file 对象存储文件
*/
public void fileStoreBySerialize(File file) {
...
}
- 第六部,使用 ObjectInputStream() 对象输入流对对象进行复原;
// 判断文件是否存在
if (file.exists()) {
// 如果存在
// 判断是否是文件夹
if (!file.isDirectory()) {
// 如果是文件
// 对对象进行复原
ObjectInputStream ois = null;
try {
// 若对象为空,则为复原
ois = new ObjectInputStream(new FileInputStream(file));
Object o1 = ois.readObject();
System.out.println("对象复原成功!");
System.out.println(o1);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// 如果是文件夹
System.out.println("该文件为文件夹,无法复原对象信息");
}
} else {
// 如果不存在
System.out.println("文件不存在!");
}
- 全部代码
1、使用File文件工具类进行文件指定类型文件筛选
2、使用io流对文件进行copy
3、使用序列化与反序列化对对象进行存储及复原
/**
* 1、使用File文件工具类进行文件指定类型文件筛选
* 2、使用io流对文件进行copy
* 3、使用序列化与反序列化对对象进行存储及复原
*/
public class FileUtil {
/**
* 3.1、使用序列化对对象进行存储
* @param file 对象存储文件
* @param o 对象
*/
public void fileStoreBySerialize(File file, Object o){
// 判断文件是否存在
if (file.exists()) {
// 如果存在
// 判断是否是文件夹
if (!file.isDirectory()) {
// 如果是文件
// 对对象进行存储
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(o);
System.out.println(o.toString());
System.out.println("对象存储成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// 如果是文件夹
System.out.println("该文件为文件夹,无法存储对象信息");
}
} else {
// 如果不存在
System.out.println("文件不存在!");
}
}
/**
* 3.2、使用反序列化对对象进行复原
* @param file 对象存储文件
*/
public void fileStoreBySerialize(File file) {
// 判断文件是否存在
if (file.exists()) {
// 如果存在
// 判断是否是文件夹
if (!file.isDirectory()) {
// 如果是文件
// 对对象进行复原
ObjectInputStream ois = null;
try {
// 若对象为空,则为复原
ois = new ObjectInputStream(new FileInputStream(file));
Object o1 = ois.readObject();
System.out.println("对象复原成功!");
System.out.println(o1);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// 如果是文件夹
System.out.println("该文件为文件夹,无法复原对象信息");
}
} else {
// 如果不存在
System.out.println("文件不存在!");
}
}
}