我正在尝试从文件中读取对象并将它们添加到Vector中,但我一直收到错误,我无法弄清楚原因。 这是阅读功能:
public Vector<Paciente> listaPacientes (int cantidad){
Vector<Paciente> result= new Vector<Paciente>();
try{
FileInputStream f=context.openFileInput(FICHERO);
ObjectInputStream is = new ObjectInputStream(f);
int n=0;
Paciente pac;
do{
pac=(Paciente) is.readObject();
if(pac!=null){
result.add(pac);
n++;
Log.d("Número n","n="+n);
}
}while(n<cantidad && pac!=null);
f.close();
}catch(Exception e){
Log.e("Historial Clinico", e.getMessage(),e);
}
return result;
}
写作功能:
public void guardarPacientes(Paciente pac){
File file=new File(FICHERO);
if(file.exists()){
try {
FileOutputStream fi = context.openFileOutput(FICHERO, Context.MODE_APPEND);
AppendingObject os=new AppendingObject(fi);
os.writeObject(os);
os.flush();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
try{
FileOutputStream f=context.openFileOutput(FICHERO, Context.MODE_APPEND);
ObjectOutputStream os = new ObjectOutputStream(f);
os.writeObject(pac);
os.flush();
os.close();
}catch (Exception e){
Log.e("Historial Clinico",e.getMessage(),e);
}
}
}
正如您所看到的,如果文件已经存在,我将从另一个类实例化,以避免标题。 所以,这是另一个类:
public class AppendingObject extends ObjectOutputStream{
public AppendingObject(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
reset();
}
这是错误:
08-02 19:21:46.365: E/Historial Clinico(32309): Wrong format: ac 08-02 19:21:46.365: E/Historial Clinico(32309): java.io.StreamCorruptedException: Wrong format: ac 08-02 19:21:46.365: E/Historial Clinico(32309): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701) 08-02 19:21:46.365: E/Historial Clinico(32309): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814) 08-02 19:21:46.365: E/Historial Clinico(32309): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006) 08-02 19:21:46.365: E/Historial Clinico(32309): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963) 08-02 19:21:46.365: E/Historial Clinico(32309): at com.example.historiaclinica.PacientestoFile.listaPacientes(PacientestoFile.java:60) 08-02 19:21:46.365: E/Historial Clinico(32309): at com.example.historiaclinica.cListaPacientes.onCreate(cListaPacientes.java:17) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.Activity.performCreate(Activity.java:5372) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.ActivityThread.access$700(ActivityThread.java:165) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.os.Handler.dispatchMessage(Handler.java:99) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.os.Looper.loop(Looper.java:137) 08-02 19:21:46.365: E/Historial Clinico(32309): at android.app.ActivityThread.main(ActivityThread.java:5455) 08-02 19:21:46.365: E/Historial Clinico(32309): at java.lang.reflect.Method.invokeNative(Native Method) 08-02 19:21:46.365: E/Historial Clinico(32309): at java.lang.reflect.Method.invoke(Method.java:525) 08-02 19:21:46.365: E/Historial Clinico(32309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 08-02 19:21:46.365: E/Historial Clinico(32309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 08-02 19:21:46.365: E/Historial Clinico(32309): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
发生EOFException是因为你到了文件的末尾。这是正常的。抓住并打破。您的代码似乎假设readObject()在文件末尾返回null。它没有。如果写入null,则返回null。