我有一个包含自定义服务对象的ArrayList.我想将整个ArrayList写入一个文件,然后可以读取它.
我为此尝试了Gson,但它给了我一个IllegalStateException:预期为BEGIN_ARRAY,但为STRING.我让它记录了应该是JSON的Strings,并在其中表示了很多异常(作为String中的文本..也许转换出错了吗?).
public int saveListToFile(){
String filename = "service_entries";
File file = new File(getFilesDir(), filename);
try {
BufferedWriter buffWriter = new BufferedWriter(new FileWriter(file, true));
Gson gson = new Gson();
String json = gson.toJson(services); //this is the ArrayList
buffWriter.append(json);
buffWriter.newLine();
buffWriter.close();
} catch (IOException e) {
return -1;
}
return 0;
}
public int readCurrentList(){
String filename = "service_entries";
File file = new File(getFilesDir(), filename);
try {