簡體   English   中英

使用RandomAccessFile刪除Java 6文件

[英]Java 6 File Deletion with RandomAccessFile

我真的很失望地討論類似的Java 6文件刪除問題,並且無法在flie刪除Post1Post2的這些類似帖子上找到解決方案。

我正在開發一個從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());
        }

    }
}

如果您在此處拋出任何未經檢查的異常,則不會關閉文件。

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塊中關閉資源,以便始終將其關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM