【问题标题】:Android loading from file ErrorAndroid从文件加载错误
【发布时间】:2011-06-01 15:11:43
【问题描述】:

当我尝试从文件中读取序列化对象时,偶尔会收到错误消息。它在 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 来读取文件。但是我不确定如何去做。任何帮助将不胜感激。

【问题讨论】:

    标签: java android serialization buffer ioexception


    【解决方案1】:

    尝试将finally 块放在try 之后,并为您的流添加结束语句,还有一个有用的方法是使用:

    FileInputStream.getFD().sync();
    

    它确保该文件真正收到您的关闭/刷新

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      相关资源
      最近更新 更多