我已将对象附加到现有文件但我无法读取它,我可以读取第一个对象,这是我的代码 有什么问题??
try{
FileOutputStream fos = openFileOutput("f.txt",MODE_PRIVATE | MODE_APPEND );
ObjectOutputStream oos = new ObjectOutputStream(fos);
String a=new String ("Hello object1 ");
String b=new String("Hello object2 ");
String c=new String("Hello object3 ");
oos.writeObject(a);
oos.writeObject(b);
oos.writeObject(c);
oos.close();
// Reading it back..
FileInputStream fis = openFileInput("f.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
//ois=new ObjectInputStream(fis);
// r=(String)ois.readObject();
String r;
while ((r= (String)ois.readObject()) != null) {
Log.i("while Read r",r);
Toast.makeText(getApplicationContext(),r, Toast.LENGTH_SHORT).show();
}
ois.close();
}catch (Exception e){
Log.i("Exception",e.getMessage());
}
我希望你能帮助我!!感谢。
答案 0 :(得分:1)
代码似乎正确只是在“While”中的一些修改,请检查以下内容:
try{
FileOutputStream fos = openFileOutput("f.txt",MODE_PRIVATE | MODE_APPEND );
ObjectOutputStream oos = new ObjectOutputStream(fos);
String a=new String ("Hello object1 ");
String b=new String("Hello object2 ");
String c=new String("Hello object3 ");
oos.writeObject(a);
oos.writeObject(b);
oos.writeObject(c);
oos.close();
// Reading it back..
FileInputStream fis = openFileInput("f.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
//ois=new ObjectInputStream(fis);
// r=(String)ois.readObject();
String r;
while (fis.available() > 0) {
r= (String)ois.readObject();
Log.i("while Read r",r);
Toast.makeText(getApplicationContext(),r, Toast.LENGTH_SHORT).show();
}
ois.close();
}catch (Exception e){
Log.i("Exception",e.getMessage());
}