序列化集合
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class Demo2 {
public static void main(String[ ] args) throws Exception {
Student student = new Student("老王", "laow") ;
Student student2 = new Student("老张", "laoz") ;
Student student3 = new Student("老李", "laol") ;
ArrayList<Student> arrayList = new ArrayList<>() ;
arrayList. add(student) ;
arrayList. add(student2) ;
arrayList. add(student3) ;
serializ(arrayList) ;
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.txt") ) ;
ArrayList<Student> list = (ArrayList<Student>) ois. readObject() ;
for (int i = 0; i < list. size() ; i++ ) {
Student s = list. get(i) ;
System. out. println(s. getName() +"‐‐"+ s. getPwd() ) ;
}
}
private static void serializ(ArrayList<Student> arrayList) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.txt") ) ;
oos. writeObject(arrayList) ;
oos. close() ;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35