Ответ 1
Возможно, это потому, что вы удаляете файл по имени inFileName перед закрытием FileInputStream, который вы открыли из него. Если вы переместите свое удаление после закрытия, тогда оно должно работать.
Ниже приведено исключение: 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);
}
}
}
Я получаю исключение только на устройстве Android 3.0, когда пытаюсь закрыть bufferedInputStream
в последнем блоке finally.
Возможно, это потому, что вы удаляете файл по имени inFileName перед закрытием FileInputStream, который вы открыли из него. Если вы переместите свое удаление после закрытия, тогда оно должно работать.