对象输入输出流

package ObjectIO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ObjectDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//method();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“e.txt”));
Object obj = ois.readObject();
ArrayList s =(ArrayList) obj;
for (Student student : s) {
System.out.println(student);
}
ois.close();

}

private static void method() throws IOException, FileNotFoundException {
    ObjectOutputStream ojs = new ObjectOutputStream(new FileOutputStream("e.txt"));
    Student s1 = new Student("zhangsan",20);
    Student s2 = new Student("lisi",18);
    Student s3 = new Student("wangwui",17);

    ArrayList<Student> list = new ArrayList<Student>();
    list.add(s1);
    list.add(s2);
    list.add(s3);
    ojs.writeObject(list);

    ojs.close();
}

}

猜你喜欢

转载自blog.csdn.net/StreamlineWq/article/details/81671730