私のアプリケーションでは、セッション間のファイルでArrayListのデータを保持します。クラスIはSerializableを実装しています。私が保存をデバッグするときに例外がスローされずにOKになり、ループを適切な回数実行しているようです。ロードすると、エントリの一部だけが読み込まれ、EOF例外がスローされます。コードはここにある:AndroidファイルI/O
public int saveChildren(Context context){
FileOutputStream fos;
ObjectOutputStream os;
try {
fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
os = new ObjectOutputStream(fos);
for(Child c : children){
os.writeObject(c);
}
os.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 2;
}
return 0;
}
public void loadChildren(Context context){
FileInputStream fis;
try {
fis = context.openFileInput(filename);
ObjectInputStream is;
is = new ObjectInputStream(fis);
while(is.readObject() != null){
Child c = (Child) is.readObject();
boolean skip = false;
for(Child ch: children){
if(ch.getName().equals(c.getName())){
skip = true;
}
if(ch.getNr().equals(c.getNr())){
skip = true;
}
if(ch.getImei() != null){
if(ch.getImei().equals(c.getImei())){
skip = true;
}
}
}
if(!skip){
children.add(c);
}
}
is.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
エラーの原因は何?
私はそれを試みましたが、結果としてアクティビティがシャットダウンしました。 – hubert
修正、それは動作します。以前のフォーマットでシリアル化されたデータをロードしようとしたため、以前は失敗しました。本当にありがとう。 – hubert
それが助けて嬉しい:) – azertiti