我有以下方法。此方法的目的是从缓存中检索存储的数组列表。
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>
再次,文件和我之前写的文件一样。但是我不明白我做错了什么。谁能帮我?