以下是我得到的例外: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, 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设备上获得例外。
答案 0 :(得分:0)
也许是因为您在关闭从中打开的FileInputStream之前删除了inFileName中的文件。如果你在关闭后移动你的删除,那么它应该工作。