Android 关闭BufferedInputStream时发生IOException
以下是我得到的异常:Android 关闭BufferedInputStream时发生IOException,android,Android,以下是我得到的异常:java.io.IOException:I/O错误 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/Sy
java.io.IOException:I/O错误
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, 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);
}
}
}
只有在Android 3.0设备上,当我尝试在最后一个finally块中关闭
bufferedInputStream
时,才会出现异常。可能是因为您在关闭从中打开的FileInputStream之前删除了InfieName中的文件。如果在关闭后移动删除,那么它应该可以工作 但是当我在finally块中说bin.close()时,我得到了一个异常。而且,我只在Android 3.0上得到了这个异常,但是在删除文件之后,在关闭基于BufferedInputStream的FileInputStream之后,您正在关闭BufferedInputStream对象。