package cn.happy6;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Day01 {
/**
* 序列化
* @throws IOException
*/
public static void main(String[] args) throws IOException {
List<Student> list=new ArrayList<Student>();
Student s1=new Student("浅蓝",20);
Student s2=new Student("杨昊鹏",20);
list.add(s1);
list.add(s2);
OutputStream os=new FileOutputStream("D:/save.bin");
//java 提供类是 输出流 ObjectOutputStream
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(list);
oos.close();
os.close();
System.out.println("serialize ok!");
}
}
package cn.happy6;
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
转载请注明原文地址: https://ju.6miu.com/read-675703.html