empty
public class 序列化 {
public static void main(String[] args) throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\789.zqf"));
//Student stu = new Student("小明",22,"郑州大学");
Student[] stu = {new Student("小明",22,"郑州大学"),new Student("小张",23,"河南大学"),new Student("小王",22,"农业大学")};
List
list = new ArrayList
();
list.addAll( Arrays.asList(stu));
oos.writeObject(list);
oos.close();
}
}
empty
public class 反序列化 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\789.zqf"));
List
list = (List
) ois.readObject();
for (Student s : list) {
System.out.println(s.name+"--"+s.age+"--"+s.className);
}
// for (Student s : stu) {
// System.out.println(s.name+"--"+s.age+"--"+s.className);
// }
}
}
序列化 是把对象或者对象的集合给序列化保存成一个文件,后缀名任意,反序列化就是把序列化的文件给读取出来!
转载请注明原文地址: https://ju.6miu.com/read-1125051.html