2016-02-26 52 views
1

一直呆在这几天,这一切似乎对我来说。我目前甚至无法看到在Android设备监视器中创建的文件。如何读取/写入内部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; 
} 

活动类 < - 控制所有事件,这个类是使用我的MainActivity类别

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; 



} 

回答

0

它似乎theObject.writeObject(oos);应改为oos.writeObject(theObject);

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

+0

谢谢,我改变了你说的话,它可能有所帮助。我似乎不断在我的日历上显示一个事件,而其他人则不保存/加载。明天我需要看看这个更近的。 – BetaBiceps

0

将新对象追加到序列化文件会损坏它,因为它会写入特定元数据的每个对象。您可以通过Object Serialization Stream Protocol了解更多信息。

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

或者因为你的对象格式很简单,你甚至可以将它序列化为JSON,并在每个对象之后用独特的分隔符将它们逐个添加到文件中。要阅读对象,你应该从这个文件中按分隔符分割String,并将每个JSON分别解析为你的对象。