标签:序列化 jdk 保存 fileinput exce dem org this over
1 package com.itheima.demo04.ObjectStream; 2 3 import java.io.*; 4 import java.util.ArrayList; 5 6 /* 7 练习:序列化集合 8 当我们想在文件中保存多个对象的时候 9 可以把多个对象存储到一个集合中 10 对集合进序列化和反序列化 11 分析: 12 1.定义一个存储Person对象的ArrayList集合 13 2.往ArrayList集合中存储Person对象 14 3.创建一个序列化流ObjectOutputStream对象 15 4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化 16 5.创建一个反序列化ObjectInputStream对象 17 6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合 18 7.把Object类型的集合转换为ArrayList类型 19 8.遍历ArrayList集合 20 9.释放资源 21 */ 22 public class Demo03Test { 23 public static void main(String[] args) throws IOException, ClassNotFoundException { 24 //1.定义一个存储Person对象的ArrayList集合 25 ArrayList<Person> list = new ArrayList<>(); 26 //2.往ArrayList集合中存储Person对象 27 list.add(new Person("张三",18)); 28 list.add(new Person("李四",19)); 29 list.add(new Person("王五",20)); 30 //3.创建一个序列化流ObjectOutputStream对象 31 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("10_IO\\list.txt")); 32 //4.使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化 33 oos.writeObject(list); 34 //5.创建一个反序列化ObjectInputStream对象 35 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("10_IO\\list.txt")); 36 //6.使用ObjectInputStream对象中的方法readObject读取文件中保存的集合 37 Object o = ois.readObject(); 38 //7.把Object类型的集合转换为ArrayList类型 39 ArrayList<Person> list2 = (ArrayList<Person>)o; 40 //8.遍历ArrayList集合 41 for (Person p : list2) { 42 System.out.println(p); 43 } 44 //9.释放资源 45 ois.close(); 46 oos.close(); 47 } 48 }
1 package com.yhqtv.demo06.trycatch; 2 3 import java.io.*; 4 import java.util.ArrayList; 5 6 /* 7 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 8 * @create 2020-05-13-18:41 9 * 10 */ 11 public class Test01 { 12 public static void main(String[] args) throws IOException, ClassNotFoundException { 13 ArrayList<Student> list=new ArrayList<>(); 14 15 list.add(new Student("tom",18)); 16 list.add(new Student("jack",13)); 17 list.add(new Student("lucy",22)); 18 list.add(new Student("lily",15)); 19 20 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\list.txt")); 21 oos.writeObject(list); 22 23 ObjectInputStream ois=new ObjectInputStream(new FileInputStream("C:\\list.txt")); 24 Object o = ois.readObject(); 25 ArrayList o1 = (ArrayList) o; 26 27 for (Object o2 : o1) { 28 System.out.println(o2); 29 } 30 31 oos.close(); 32 ois.close(); 33 34 } 35 }
1 package com.yhqtv.demo06.trycatch; 2 3 import jdk.internal.org.objectweb.asm.commons.SerialVersionUIDAdder; 4 5 import java.io.Serializable; 6 7 /* 8 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 9 * @create 2020-05-13-18:44 10 * 11 */ 12 public class Student implements Serializable { 13 private static final long serialVersionUID=23432L; 14 private String name; 15 private int age; 16 17 public Student() { 18 } 19 20 public Student(String name, int age) { 21 this.name = name; 22 this.age = age; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 @Override 42 public String toString() { 43 return "Student{" + 44 "name=‘" + name + ‘\‘‘ + 45 ", age=" + age + 46 ‘}‘; 47 } 48 }
标签:序列化 jdk 保存 fileinput exce dem org this over
原文地址:https://www.cnblogs.com/yhqtv-com/p/12884337.html