2011-12-27 60 views
2

我真的很失望,討論一個類似的Java 6文件刪除問題,無法找到這些類似的帖子上刪除Post1Post2這些類似的帖子上的解決方案。用隨機訪問文件刪除Java 6文件

我正在從FTP服務器下載文件的應用程序。此下載是通過獲取流,從此流中讀取並將其寫入文件RandomAccessFile來實現的。如果下載損壞,我希望刪除磁盤上的文件。

我無法手動或通過應用程序刪除該文件。很明顯,某些文件處理程序仍然因文件刪除失敗而導致文件鎖定。 但是,我無法理解哪些文件處理程序擁有文件鎖,因爲我已確保關閉所有文件和流對象。

最後,該代碼段

public class OprDownload extends Observable implements Runnable 
{ 
    public void run() 
    { 
    //Code to connect to ftp,obtain the stream and write to a file 
    if (download complete) 
    { 

     if(mwwObjFile!=null) 
     { 
      mwwObjFile.close(); 

     } 
     if(stream!=null) 
     { 
      stream.close(); 
     } 
     if(compareChecksum())//If checksum match success 
     { 
      updateDownloadDetails(); 
      cwwIntStatus = COMPLETE; 
     } 
     else 
     { 
      cwwIntStatus = CHECKSUM_FAIL; 
      deleteCorruptUpdateFromDownloadsFolder(); 
     } 
    } 
} 

    public void deleteCorruptUpdateFromDownloadsFolder() 
    { 
     String fileOndisk = "FileName"; 
     File mwwFileToBeDeleted = new File(fileOndisk); 

     if(mwwFileToBeDeleted .exists()) 
     { 
      System.out.println("File Deleted"+mwwFileToBeDeleted .delete()); 
     } 

    } 
} 
+2

是否從文件中讀取比較校驗和代碼?如果是,它是否關閉所有流? – 2011-12-27 07:36:29

+0

賓果! 好吧我不相信我沒有關閉比較校驗和代碼中的文件對象。謝謝 – shabeena 2011-12-27 07:41:09

回答

1

如果您在此處拋出任何未檢查的異常,該文件(S)將不會被關閉。

public void run() 
{ 
    //Code to connect to ftp,obtain the stream and write to a file 
    if (download complete) // throws an unchecked exception and exits run() 
    { 

您需要關閉finally塊中的資源,以便它們始終關閉。

+1

是的,我已經關閉了在finally代碼塊中的資源在實際code.Have編輯它在這裏使短片段short.Thanks無論如何。 – shabeena 2011-12-27 11:29:35