将自定义对象的ArrayList写入File [英] Write ArrayList of custom objects to File
问题描述
我有一个包含自定义服务对象的ArrayList。我想将整个ArrayList写入一个文件,然后能够读取它。
我试过Gson,但它给了我一个IllegalStateException:期望的BEGIN_ARRAY,但是串。我让它记录了JSON的字符串,它说了很多Exceptions(作为String中的文本..也许转换出了问题?)。
public int saveListToFile(){
String filename =service_entries;
File file = new File(getFilesDir(),filename);
尝试{
BufferedWriter buffWriter = new BufferedWriter(new FileWriter(file,true));
Gson gson = new Gson();
String json = gson.toJson(services); //这是ArrayList< Service>
buffWriter.append(json);
buffWriter.newLine();
buffWriter.close();
} catch(IOException e){
return -1;
}
返回0;
public int readCurrentList(){
String filename =service_entries;
File file = new File(getFilesDir(),filename);
尝试{
BufferedReader buffReader = new BufferedReader(new FileReader(file));
String line1,line2;
Gson gson = new Gson();
line1 = buffReader.readLine();
line2 = buffReader.readLine();
if(line1 == null){
buffReader.close();
返回0;
}
Type type = new TypeToken< ArrayList< Service>>(){}。getType();
services = gson.fromJson(line1,type);
ArrayList< Service>列表2;
if(line2!= null){
list2 = gson.fromJson(line2,type);
services.addAll(list2);
list2 = null;
}
buffReader.close();
} catch(IOException e){
return -1;
}
返回0;
}
公共类服务{
私人双倍数量;
私有字符串描述;
公共服务(){
数量= 0.0;
description = null;
}
}
我会建议使用序列化进行这样的操作。你可以实现Java的Serializable接口,然后你可以将你的对象压缩成一个.ser文件,然后从这个文件中将它们膨胀回来,从而调用你需要的方法。
这是一个关于序列化的好教程 - http://www.tutorialspoint.com/java/java_serialization.htm
I have an ArrayList which contains custom Service objects. I want to write the whole ArrayList to a File and be able to read it afterwards.
I tried Gson for that, but it gives me a IllegalStateException: Expected BEGIN_ARRAY but was STRING. I let it log me the Strings of what should be JSON and it said a lot of Exceptions in it (as a text inside the String.. maybe the conversion has gone wrong?).
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<Service>
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 {
BufferedReader buffReader = new BufferedReader(new FileReader(file));
String line1, line2;
Gson gson = new Gson();
line1 = buffReader.readLine();
line2 = buffReader.readLine();
if(line1 == null){
buffReader.close();
return 0;
}
Type type = new TypeToken<ArrayList<Service>>(){}.getType();
services = gson.fromJson(line1, type);
ArrayList<Service> list2;
if(line2 != null){
list2 = gson.fromJson(line2, type);
services.addAll(list2);
list2 = null;
}
buffReader.close();
} catch(IOException e){
return -1;
}
return 0;
}
public class Service {
private double quantity;
private String description;
public Service(){
quantity = 0.0;
description = null;
}
}
I would recommend using serialization for such an operation. You can implement Java's Serializable interface and you are then able to deflate your objects into a .ser file and then inflate them back from that very file to call the methods you need from them.
Here is a nice tutorial regarding Serialization - http://www.tutorialspoint.com/java/java_serialization.htm
这篇关于将自定义对象的ArrayList写入File的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!