私は次の方法を持っています。このメソッドの目的は、格納された配列リストをキャッシュから取得することです。
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>
繰り返しますが、ファイルは以前に書いたファイルと同じです。しかし、私は自分が間違っていることを理解できません。誰でも私を助けることができますか?