跳过正文
  1. 文章/
  2. 代码块/
  3. Java代码/

序列化及反序列化

·86 字·1 分钟· loading · loading · ·
代码块 Java代码
GradyYoung
作者
GradyYoung
Java代码 - 点击查看当前系列文章
§ 序列化及反序列化 「 当前文章 」
//序列化
public static void serialization(List list,String file) throws IOException {
    File file1 = new File(file);
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
    for (Object o : list){
        outputStream.writeObject(o);
    }
    outputStream.flush();
    outputStream.close();
}
//反序列化
public static List deserialization(String file) throws IOException, ClassNotFoundException {
    File file1 = new File(file);
    List list = new ArrayList();
    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
    Object o;
    while (true){
        try {
            o = inputStream.readObject();
        }catch (Exception e){
            break;
        }
        list.add(o);
    }
    inputStream.close();
    return list;
}
Java代码 - 点击查看当前系列文章
§ 序列化及反序列化 「 当前文章 」