【问题标题】:IOException while closing BufferedInputStream关闭 BufferedInputStream 时出现 IOException
【发布时间】:2011-04-13 20:54:15
【问题描述】:

以下是我得到的例外: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)

以下是我的代码 sn-p:

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, 0, 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 设备上遇到异常。

【问题讨论】:

    标签: android


    【解决方案1】:

    可能是因为您在关闭从文件中打开的 FileInputStream 之前删除了 inFileName 处的文件。如果您在关闭后移动删除,那么它应该可以工作。

    【讨论】:

    • 但是当我在 finally 块中说 bin.close() 时出现异常。
    • 而且,我只在 Android 3.0 上得到这个异常
    • 但是在您删除文件后关闭了 BufferedInputStream 对象,并且在您关闭了基于 BufferedInputStream 的 FileInputStream 之后。
    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 2016-02-07
    • 2011-09-10
    • 2017-10-15
    • 1970-01-01
    • 2014-02-22
    • 2021-12-13
    • 2019-09-24
    相关资源
    最近更新 更多