importjava.io.*;importjava.util.*;classfile{Filef=null;FileInputStreamfips=null;ObjectOutputStreamoops=null;publicfile()throwsException{f=newFile("d:"+File.separator+"res...
import java.io.*;
import java.util.*;
class file
{
File f=null;
FileInputStream fips=null;
ObjectOutputStream oops=null;
public file()throws Exception{
f=new File("d:"+File.separator+"resource.txt");
if(!f.exists())
f.createNewFile();
fips=new FileInputStream(f);
oops=new ObjectOutputStream(new FileOutputStream(f));
if(fips.read()==-1){
fips.close();
List l=new ArrayList();
oops.writeObject(l);
oops.close();
}
}
public void save(Object o)throws Exception{
ObjectOutputStream oops1=null;
oops1=new ObjectOutputStream(new FileOutputStream(f));
@SuppressWarnings("unchecked")
List per=(List)this.load();
per.add((Person)o);
oops.writeObject(per);
oops.close();
}
public Object load()throws Exception{
ObjectInputStream oips=new ObjectInputStream(new FileInputStream(f));
@SuppressWarnings("unchecked")
List per2=(List)oips.readObject();
oips.close();
return per2;
}
}
public class main
{
public static void main(String[] args)throws Exception
{
file f=new file();
f.save(new Person("小明",12));
f.save(new Person("小红",11));
}
}
出现java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Sourc
e)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at file.load(file.java:33)
at file.save(file.java:25)
at main.main(main.java:6)
求高手指点!!!!!
展开