-1

私は次の方法を持っています。このメソッドの目的は、格納された配列リストをキャッシュから取得することです。

  public ArrayList<T> read_arraylist_from_cache(File name){

    FileInputStream fis;
    ArrayList<T> returnlist = null;
    try {   
            fis = new FileInputStream(name);            
            ObjectInputStream ois = new ObjectInputStream(fis);

           //here I take the exception 
            returnlist = (ArrayList<T>) ois.readObject();
            ois.close();
    } catch (Exception e) {
        Log.d("Cannot retrieve the arraylist from the cache", "Cannot retrieve the arraylist from the cache", e);
        e.getStackTrace();
    }
    return returnlist;
}

ただし、ArrayList にキャストしようとすると、WriteAbortedException が発生します。前述のクラスに渡す ArrayList は次のとおりです。

 ArrayList <Product>

製品 pojo の場所:

 public class Products implements Serializable{

 @JsonProperty
 private String prodnum;
     @JsonProperty
     private String brand;

      //get,set
  }

配列リストをキャッシュに保存するために使用している方法は次のとおりです

   public void write_to_byte_array(ArrayList<T> list,File file){

    // write to byte array
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
        Log.d("file not found", "file not found", e1);
    }

    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(fos);
    } catch (Exception e1) {
        Log.d("Create ObjectOutputStream object", "Create ObjectOutputStream object", e1);
    }


    try {
        oos.writeObject(list);
    } catch (IOException e1) {
        Log.d("Write ObjectOutputStream object to file", "Write ObjectOutputStream object to file", e1);
    }


    try {
        oos.close();
    } catch (IOException e1) {
        Log.d("Close the connection with the file", "Close the connection with the file", e1);
    }


}

前述のメソッドに渡す arratList リストは、

        ArrayList<Products>

繰り返しますが、ファイルは以前に書いたファイルと同じです。しかし、私は自分が間違っていることを理解できません。誰でも私を助けることができますか?

4

1 に答える 1

1

WriteAbortedException APIは、この問題をかなり詳しく説明しています。

読み取ろうとしているオブジェクトを書き込んでいるときに、ログに何かをキャッチしましたか?

WriteAbortedException.getCause() によって何が返されるかを確認しましたか?

また、注意してください。あなたの例外処理はwrite_to_byte_array本当に再考する必要があります。FileOutputStream コンストラクターが失敗し、ファイルが見つからない場合は、ログに記録し、何も問題がないかのように続行します。これにより、次の try ブロックで NullPointerException がスローされることが保証され、何もないかのようにもう一度続行されます。次の try ブロックで別の NullPointerException を保証します。

于 2012-12-11T21:15:06.203 に答える