在Android中保存对象或对象列表

时间:2017-05-21 01:55:36

标签: android

我有一个自定义对象类的列表,我称之为学生,我希望以一种方式保存它,以便在我们完全关闭应用程序时不会受到影响。这个类:(当然我有一个构造函数和东西,但这些是可能很重要的字段)

public class Students implements Serializable{
private int ID;
private int imageId;
private String firstName;
private String lastName;
private Map<Date, Boolean> Attendance;

我尝试保存和阅读它(在活动中):

public Boolean saveStudent(Students s) {//saves student into fileName (data.bin)
    ObjectOutputStream oos = null;
    try {
        File file = new File(this.getFilesDir().toString(), fileName);
        file.createNewFile();
        if(!file.mkdir()){ //just to check whats the reason behind the failed attempt
            Toast.makeText(this, "Security Issue", Toast.LENGTH_SHORT).show();
        }
        FileOutputStream fos = this.openFileOutput(fileName, this.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(s);
        oos.close();
        fos.close();
        return true;
    } catch (FileNotFoundException err) {
        Toast.makeText(this, "Something went wrong while saving", Toast.LENGTH_SHORT).show();
        return false;
    } catch (Exception abcd) {
        Toast.makeText(this, "Ooops, I don't know what's the problem. Sorry about that!", Toast.LENGTH_SHORT).show();
        return false;
    }
    finally {//makes sure to close the ObjectOutputStream
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


public Students readStudent() {//reads Student object from(data.bin) and returns it.
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream("data.bin"));
        ois.close();
        return (Students) ois.readObject();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

当我跑步时,它给了我&#34;在保存时出现问题&#34;和#34;安全问题&#34;。有人可以帮我解决这个问题吗?

PS:我真的不想使用SQLlite数据库,因为我认为仅仅3或4个条目就是一种过度杀伤力。我希望这不是一个需要花费几个小时才能发现的愚蠢错误之一。

2 个答案:

答案 0 :(得分:0)

请检查此来源。我测试了它,它运行良好。

{{1}}

要保存和加载多个对象,请查看此链接How do I save and multiple objects from a single file?

答案 1 :(得分:0)

无需file.mkdir()

openFileOutput一个绝对路径可能是好的。

点击此处:

File file = new File(this.getFilesDir().toString(), fileName);
file.createNewFile();
//if(!file.mkdir()){ //just to check whats the reason behind the failed attempt
//    Toast.makeText(this, "Security Issue", Toast.LENGTH_SHORT).show();
//}
// Use file.getAbsolutePath()
FileOutputStream fos = this.openFileOutput(file.getAbsolutePath(), this.MODE_PRIVATE);
oos =oos = new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
fos.close();
return true;

在read方法中,你应该给出一个绝对文件路径

检查此行:ois = new ObjectInputStream(new FileInputStream("data.bin"));

将更多信息传递给new FileInputStream("data.bin")

更改为:new FileInputStream(new File(getContext().getFilesDir(),"data.bin"));

不要忘记父目录。