解决循环操作ObjectInputStream的readObject()方法的EOF异常

Code:
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.io.Serializable; 
 
public class ObjectStream { 
    public static void main(String[] args) { 
        ObjectOutputStream oos = null; 
        ObjectInputStream ois = null; 
        Person pe = null; 
        try { 
            FileOutputStream out = new FileOutputStream("D:/obj.txt"); 
            oos = new ObjectOutputStream(out); 
            Person per = new Person("微软", 14); 
            pe = new Person("金山", 20); 
            oos.writeObject(per); 
            oos.writeObject(pe); 
             oos.writeObject(null);//解决EOF的关键,加入一个空的对象 
            System.out.println("添加成功"); 
            oos.close(); 
            out.close(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
         
        try{ 
            FileInputStream in = new FileInputStream("D:/obj.txt"); 
            ois = new ObjectInputStream(in); 

你可能感兴趣的:(解决循环操作ObjectInputStream的readObject()方法的EOF异常)