出现java.io.EOFException 求教!!
importjava.io.*;importjava.util.*;classfile{Filef=null;FileInputStreamfips=null;Objec...
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<Person> l=new ArrayList<Person>();
oops.writeObject(l);
oops.close();
}
}
public void save(Object o)throws Exception{
ObjectOutputStream oops1=null;
oops1=new ObjectOutputStream(new FileOutputStream(f));
@SuppressWarnings("unchecked")
List<Person> per=(List<Person>)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<Person> per2=(List<Person>)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)
求高手指点!!!!! 展开
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<Person> l=new ArrayList<Person>();
oops.writeObject(l);
oops.close();
}
}
public void save(Object o)throws Exception{
ObjectOutputStream oops1=null;
oops1=new ObjectOutputStream(new FileOutputStream(f));
@SuppressWarnings("unchecked")
List<Person> per=(List<Person>)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<Person> per2=(List<Person>)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)
求高手指点!!!!! 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询