java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
代碼
import java.io.File;
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;
public class Data {
private File file=new File("e:"+File.separator+"message.txt");
private ObjectInputStream ois;
private ObjectOutputStream oos;
public ArrayList read() throws FileNotFoundException, IOException{
ArrayList list=new ArrayList();
ois=new ObjectInputStream(new FileInputStream(file));
try {
list=(ArrayList<Message>) ois.readObject();//報錯的位置
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void write(ArrayList<Message> list){
try {
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ois=new ObjectInputStream(new FileInputStream(file));
這一句返回值可能為空,或者是下一句無法讀到東西;
強轉崩潰;