ArrayList<personList> list = new ArrayList<>();
list.add(new personList("张三",18));
list.add(new personList("赵四",18));
list.add(new personList("王五",18));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("IO\\list.txt"));
oos.writeObject(list);将数组输入到oos文件流中
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("IO\\list.txt"));
读取list.txt文件
Object o =ois.readObject();读取字符串输入到自定义类中
ArrayList<personList> lists2 = (ArrayList<personList>)o;
将自定义类强转为ArrayList,方便遍历输出
for (personList p : lists2) {
System.out.println(p);
}
ois.close();
oos.close();
将对象存储到集合当中,在输入到文件里
最新推荐文章于 2022-05-12 17:05:05 发布