2014-03-29 98 views
0

下面是我的JAVA代碼用於提取和刪除文件。問題是,當我成功地提取zip文件,然後當我刪除它。它不會起作用。可以提取文件但不能刪除文件

注:
地方=我的本地文件夾
zip文件=我的zip文件名
的拉鍊和TXT文件是從FTP服務器上下載的java程序
我可以手動從我的資源管理器中刪除,但我的程序

public static void extract(File local, String zipFile) { 

    try { 
     // destination folder to extract the contents   
     String destName = local + "";  

     byte[] buf = new byte[1024]; 
     ZipInputStream zis = null; 
     ZipEntry zipentry; 
     zis = new ZipInputStream(new FileInputStream(local + "/" + zipFile)); 
     zipentry = zis.getNextEntry(); 

     while (zipentry != null) { 
      // for each entry to be extracted 
      String entryName = zipentry.getName(); 

      int n; 
      FileOutputStream fos3; 
      File newFile = new File(entryName); 

      String directory = "/tmp/"; 

      // to creating the parent directories 
      if (directory == null) { 
       if (newFile.isDirectory()){ 
        break; 
       } 
      } else { 
       new File(destName+directory).mkdirs(); 
      } 

      if(!zipentry.isDirectory()){ 
       System.out.println("File to be extracted....."+ entryName); 
       fos3 = new FileOutputStream(destName + directory + entryName); 

       while ((n = zis.read(buf, 0, 1024)) > -1){ 
        fos3.write(buf, 0, n); 
       } 
       fos3.close(); 
      } 

      zis.closeEntry(); 
      zipentry = zis.getNextEntry(); 
     }// while 
     zis.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
public static void delete(File file) { 

    // Check if file is directory/folder 
    if(file.isDirectory()) { 
     // Get all files in the folder 
     File[] files=file.listFiles(); 

     for(int i=0;i<files.length;i++) { 

      // Delete each file in the folder 
      delete(files[i]); 
      System.out.println("Successfully delete file --> " + files[i]); 
     } 

     // Delete the folder 
     file.delete(); 
    }else { 
     // Delete the file if it is not a folder 
     file.delete(); 
     System.out.println("Successfully delete file --> " + file); 
    } 
} 
public static void main(String[] args) { 
//extract 
extract(local, zipFile); 

File ftpClientZip = new File(local + "/" + zipFile); 
File ftpClientMD5 = new File(local + "/" + txtFile); 

delete(ftpClientZip); 
delete(ftpClientMD5); 
} 
+0

有沒有什麼異常? – dimoniy

+0

這只是我的代碼的一部分。我的完整程序運行流程如下: 連接ftp - >下載文件 - >檢查zip文件md5 - >解壓縮zip文件 - >將解壓縮的文件複製到目的地 - >刪除zip文件 – tommy5115

+0

否...是否存在正在拋出的錯誤到控制檯...像「線程中的異常」主「」java.io.IOException ...「#: –

回答

0

嗯,我不確定這是否是後錯字,或者如果它是您的代碼中的錯誤。你有重複的delete()方法。

如果這不是問題,我想知道,如果你想根據從壓縮文件內的指針創建文件...

如果沒有,我沒有看到一個問題

+0

我只是編輯帖子,仍然沒有錯誤從我的代碼? – tommy5115

1

Files類提供了兩種刪除方法。

刪除(Path)方法刪除文件或拋出異常如果刪除失敗。例如,如果該文件不存在,則拋出NoSuchFileException。您可以捕獲該異常,以確定爲什麼刪除失敗如下:

try { 
    Files.delete(path); 
} catch (NoSuchFileException x) { 
    System.err.format("%s: no such" + " file or directory%n", path); 
} catch (DirectoryNotEmptyException x) { 
    System.err.format("%s not empty%n", path); 
} catch (IOException x) { 
    // File permission problems are caught here. 
    System.err.println(x); 
} 

的deleteIfExists(path)方法還刪除了文件,但如果文件不存在,不會引發任何異常。如果您有多個線程刪除文件,並且您不想僅因爲一個線程首先執行而引發異常,那麼靜默失敗非常有用。

+0

Files.delete(path); 「文件」是方法名稱? 我複製並粘貼,它有錯誤。 – tommy5115

+0

最新錯誤 –

+0

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html它的一個在java.lang.Object包中的構建類包 –

0

檢查文件/包含文件夾權限。 可能是運行java程序的進程有讀取權限但沒有寫權限

+0

我檢查所有zip文件和txt文件,文件權限。 canWrite,canRead,canExecute all tr​​ue – tommy5115

+0

在提取方法中是否有可能出現異常?如果是這樣,close()將不會發生並可能導致此情況。這是一個很遠的嘗試,但是試着把close()放在最後一塊 – danny