开发者

Android loading from file Error

开发者 https://www.devze.com 2023-03-09 15:37 出处:网络
I am getting the occassional error message when I try to read a serialized object from a file. It works fine 9 times out of 10, but for some reason I get lots 开发者_运维技巧of these error message sin

I am getting the occassional error message when I try to read a serialized object from a file. It works fine 9 times out of 10, but for some reason I get lots 开发者_运维技巧of these error message sin the catlog:

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

and

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

The second message comes with no indication where the exception is caused. (Clearly when I'm loading the file, but I already have a try/catch around it.)

My loadfile method looks like this:

     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;
    }
    }

One possibility I have thought of is using a BufferedReader to rea the file. However I'm not sure how to go about doing this. Any help would be appreciated.


Try to put finally block after try and put there closing statements for your streams and also useful thing is to use:

FileInputStream.getFD().sync();

It makes sure that file really received your close/flush

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号