2011-04-13 90 views
1

以下是例外,我得到:java.io.IOException: I/O errorIOException異常而關閉的BufferedInputStream

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

回答

0

也許這是因爲您在關閉您打開的FileInputStream之前正在刪除inFileName中的文件。如果你在關閉後移動你的刪除,那麼它應該工作。

+0

但我得到一個異常,當我說在finally塊的bin.close()。 – Shruti 2011-04-14 10:02:05

+0

此外,我僅在Android 3.0上纔得到此異常 – Shruti 2011-04-14 10:38:27

+0

但是,在您刪除文件並關閉了基於BuferedInputStream的FileInputStream之後,您正在關閉BufferedInputStream對象。 – 2011-04-14 11:46:27

相關問題