【问题标题】:How to Read/Write a Class Object to an Internal Android File?如何读取/写入类对象到内部 Android 文件?
【发布时间】:2016-02-26 06:43:03
【问题描述】:

在这个问题上坚持了几天,我觉得这一切都是正确的。我目前甚至无法在 Android 设备监视器中看到正在创建的文件。

我正在尝试一次将一个事件对象写入文件,并在任何给定时间读回所有事件。

事件类

public class Event implements Serializable {

private static final long serialVersionUID = -29238982928391l;

public String time;
public String drug;
public Date date;
public int dose;

SimpleDateFormat format = new SimpleDateFormat("MM-dd");

public Event(Date date, String time, String drug, int dose){
    this.time = time;
    this.drug = drug;
    this.date = date;
    this.dose = dose;
}

Events 类

public class Events {

ArrayList<Event> eventslist = new ArrayList<Event>();
String saveFileName = "calendarEvents.data";
Context context;



public Events(Context ctx) {
    super();
    context = ctx;
}


// Reads the events for a given day
public ArrayList<Event> readData(Date event_date) {
    ArrayList<Event> dayEvents = new ArrayList<Event>();

    for (Event entry : eventslist) {
        Date d = entry.getDate();

        if(d.compareTo(event_date) == 0){
            dayEvents.add(entry);
        }
    }
    return dayEvents;
}


public ArrayList<Event> readAllEvents() {
    return eventslist;
}


//inserts an event into the array
// this is what calls save()
public int insertData(Date date, String time, String drug, int dose) {
    Event e = new Event(date, time, drug, dose);

    try {
        save(saveFileName, e);
        eventslist.add(e);
    } catch (Exception ex){
        ex.printStackTrace();
        return -1;
    }
    return 1;
}


//My actual write function
public void save(String filename, Event theObject) {
    FileOutputStream fos;
    ObjectOutputStream oos;

    try {
        fos = context.openFileOutput(filename, Context.MODE_APPEND);
        oos = new ObjectOutputStream(fos);

        theObject.writeObject(oos);
        oos.close();
        fos.close();
    } catch(IOException e){
        e.printStackTrace();
    }
}

//my read function, not called in this example
public ArrayList<Event> readFile(String filename, Context ctx) {
    FileInputStream fis;
    ObjectInputStream ois;
    ArrayList<Event> ev = new ArrayList<Event>();

    try {
        fis = ctx.openFileInput(filename);
        ois = new ObjectInputStream(fis);

        while(true) {
            try{
                ois.readObject();
                //ev.add();
            } catch (NullPointerException | EOFException e){
                e.printStackTrace();
                ois.close();
                break;
            }
        }
        ois.close();
        fis.close();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }

    return ev;



}

【问题讨论】:

    标签: java android serialization


    【解决方案1】:

    看来theObject.writeObject(oos);应该改成oos.writeObject(theObject);

    我想你在Event类中定义了名为readObject/writeObject的方法。但是你应该知道这两个方法是由ObjectInputStream/ObjectOutputStream反射和调用的,你不应该直接从外面调用它(所以请把这些两个私有方法)。

    【讨论】:

    • 谢谢,我改变了你所说的,它可能会有所帮助。我的日历上似乎经常显示一个事件,而其他事件则不保存/加载。明天我需要仔细研究一下。
    【解决方案2】:

    将新对象附加到序列化文件会损坏它,因为它写入特定元数据的每个对象。你可以在Object Serialization Stream Protocol阅读更多关于它的信息。

    我建议使用另一种类型的序列化来存储对象。你可以用 JSON 做到这一点:How to insert one more item in a json existing structure with gson?

    或者由于您的对象格式非常简单,您甚至可以将其序列化为 JSON,并在每个对象之后使用唯一分隔符逐个附加到文件中。要读取对象,您只需将 String 通过分隔符从该文件中拆分出来,然后将每个 JSON 分别解析为您的对象。

    【讨论】:

      猜你喜欢
      • 2014-09-25
      • 2023-03-23
      • 2020-02-18
      • 2013-11-20
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多