【问题标题】:Android file I/OAndroid 文件 I/O
【发布时间】:2012-05-01 11:48:29
【问题描述】:

我希望我的应用在会话之间将来自 ArrayList 的数据保存在文件中。我使用的类实现了可序列化。当我调试时,保存似乎没问题,没有抛出异常,并且通过循环正确的次数。加载只加载一些条目,然后抛出 EOF 异常。代码在这里:

public int saveChildren(Context context){
    FileOutputStream fos;
    ObjectOutputStream os;
    try {
        fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
        os = new ObjectOutputStream(fos);
        for(Child c : children){
            os.writeObject(c);
        }
        os.close();
    }
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 1;
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 2;
    }
    return 0;
}

public void loadChildren(Context context){

    FileInputStream fis;
    try {
        fis = context.openFileInput(filename);
        ObjectInputStream is;
        is = new ObjectInputStream(fis);
        while(is.readObject() != null){
            Child c = (Child) is.readObject();
            boolean skip = false;
            for(Child ch: children){
                if(ch.getName().equals(c.getName())){ 
                    skip = true;
                }
                if(ch.getNr().equals(c.getNr())){ 
                    skip = true;
                }
                if(ch.getImei() != null){
                    if(ch.getImei().equals(c.getImei())){
                        skip = true;
                    }
                }
            }
            if(!skip){
                children.add(c);
            }
        }
        is.close();
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (StreamCorruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }catch (OptionalDataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

导致错误的原因是什么?

【问题讨论】:

    标签: android file serialization


    【解决方案1】:

    您使用ObjectInputStream#readObject() 返回null 作为循环的退出条件。 #readObject() 可以返回 null - 如果你已经序列化了 null - 但是如果因为你已经到达流的末尾而无法读取对象,它将抛出异常。

    为了快速简单的解决方法:考虑在序列化子元素之前将数组的长度序列化为整数,并使用 for 循环或类似结构来读取值 - 或序列化 ArrayList 本身。也是Serializable

    【讨论】:

      【解决方案2】:

      您可以直接将子对象写入 ObjectOutputStream。 ArrayList 实现可序列化。您可能想要做的第二件事是在使用 os.flush() 关闭流之前刷新流。您将拥有:

      os = new ObjectOutputStream(fos);    
      os.writeObject(children);    
      os.flush();
      os.close();
      

      阅读:

      is = new ObjectInputStream(fis);
      ArrayList<Child> children = (ArrayList<Child>)is.readObject();
      is.close();
      

      【讨论】:

      • 更正,它有效。它之前失败了,因为它试图加载以旧格式序列化的数据。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2012-06-18
      • 2013-06-30
      • 2010-12-25
      • 1970-01-01
      • 2013-09-22
      • 2015-11-13
      • 2016-02-09
      • 2013-05-06
      相关资源
      最近更新 更多