微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IOException,同时关闭BufferedInputStream

如何解决IOException,同时关闭BufferedInputStream

| 以下是我得到的例外:
java.io.IOException: I/O error
04-13 07:11:35.040 W/System.err(  986):     at libcore.io.IoUtils.close(Native Method)
04-13 07:11:35.040 W/System.err(  986):     at java.io.FileInputStream.close (FileInputStream.java:139)
04-13 07:11:35.040 W/System.err(  986):     at java.io.BufferedInputStream.close (BufferedInputStream.java:134)
以下是我的代码段:
public static void compress(String inFileName,String outFileName) { 
    GZIPOutputStream out = null;
    BufferedInputStream bin = null;
    FileOutputStream fileOutputStream = null; 
    FileInputStream fileInputStream = null;
    File inFile = null;
    try {
        inFile = new File(inFileName);
        if(inFile.exists()) 
        {

                fileOutputStream = new FileOutputStream(outFileName);
                out = new GZIPOutputStream(fileOutputStream);


                fileInputStream = new FileInputStream(inFileName);
                bin = new BufferedInputStream(fileInputStream);




            if(inFile.exists())
            {
                inFile.delete();
            }


            // Reading from buffer and writing it to file
            byte[] buf = new byte[1024];
            FileOutputStream inFileOutputStream = null;
            int len;
            try
            {
                while((len = bin.read(buf)) > 0) {
                    out.write(buf,len);
                }

              //Close the inputstream since it is not required any more.

                fileInputStream.close();
                fileInputStream = null;

            }
            catch(Exception e)
            {


            }
            finally
            {

            }

            out.finish();
        }
        else
        {

        }
    } catch (IOException e) {
        throw new MyException(e.getMessage(),e);
    }
    catch(MotoDCException e)
    {
        throw new MyException(e.getMessage(),e);
    }
    finally
    {
        try
        {


            if(bin != null)
            {
                bin.close();
                bin = null;
            }

            if(fileOutputStream != null)
            {
                fileOutputStream.close();
                fileOutputStream = null;
            }
            if(out != null)
            {                   
                out.close();
                out = null;
            }
            inFile = null;
        }
        catch(IOException e)
        {

            throw new MyException(e.getMessage(),e);
        }
    }

}
当我尝试在最后的finally块中关闭
bufferedInputStream
时,我仅在Android 3.0设备上得到了例外。     

解决方法

可能是因为您要删除inFileName上的文件,然后再关闭从其中打开的FileInputStream。如果您在关闭后移动删除内容,则它应该可以工作。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。