Android从文件加载错误

时间:2011-06-01 15:11:44

标签: java android serialization buffer ioexception

当我尝试从文件中读取序列化对象时,我收到了偶然的错误消息。它在10个中工作了9次,但由于某些原因,我得到了很多这些错误消息,这些都是catlog:

    06-01 23:57:50.824: ERROR/MemoryFile(16077): MemoryFile.finalize() called while ashmem 
    still open

    06-01 23:57:57.664: ERROR/MemoryFile(16077): java.io.IOException: munmap failed

第二条消息没有指示导致异常的位置。 (显然当我加载文件时,我已经尝试了一下它。)

我的loadfile方法如下所示:

     public TGame loadSavedGame(){
    TGame g=null;
    InputStream instream = null;
    BufferedReader br=null;
    InputStreamReader inputreader=null;

    try {
         File sdCard = Environment.getExternalStorageDirectory();
        instream = new
              FileInputStream(sdCard.getAbsolutePath()+"/egyptica/serializationtest");
        // inputreader = new InputStreamReader(instream);
        // br= new BufferedReader(inputreader);


    } catch (FileNotFoundException e) {

        e.printStackTrace();
        return null;
    }
    try {
        ObjectInputStream ois = new ObjectInputStream(instream);
         try {
            g= (TGame) ois.readObject();

            try {
                instream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return g;
        } catch (ClassNotFoundException ex) {
            // TODO Auto-generated catch block
            android.util.Log.e("DESERIALIZATION FAILED (CLASS NOT 
          FOUND):"+ex.getMessage(), "ex");
            ex.printStackTrace();
            return null;
        }
     } catch (StreamCorruptedException ex) {
            android.util.Log.e("DESERIALIZATION FAILED (CORRUPT):"+ex.getMessage(), 
             "ex");
        // TODO Auto-generated catch block
        ex.printStackTrace();
        return null;
    } catch (IOException ex) {
            android.util.Log.e("DESERIALIZATION FAILED (IO 
                      EXCEPTION):"+ex.getMessage(), "ex");
        // TODO Auto-generated catch block
        ex.printStackTrace();
        return null;
    }
    }

我想到的一种可能性是使用BufferedReader来恢复文件。但是我不知道该怎么做。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试在finally之后放置try阻止并为您的流添加结束语句,并且有用的是使用:

FileInputStream.getFD().sync();

确保文件确实收到了关闭/刷新