2013-03-16 134 views
0
import java.io.*; 

public class FileChecker { 

    private static final String FILE_DIR = "F:/study/"; 
    private static final String FILE_TEXT_EXT1 = ".pdb"; 
    private static final String FILE_TEXT_EXT2 = ".ali"; 
    private static final String FILE_TEXT_EXT3 = ".txt"; 
    private static final String FILE_TEXT_EXT4 = ".py"; 
    private static final String FILE_TEXT_PREFIX1 = "target."; 
    private static final String FILE_TEXT_PREFIX2 = "blastOutput."; 
    //private static final String FILE_TEXT_PREFIX3 = "1OPL."; 

    //public static void main(String args[]) 
    public FileChecker() 
    { 
/*new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT1); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT2); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT3); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT4); 
     new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX1); 
     new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX2); 
     new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX3);*/ 
    } 

    public void deleteFile(String folder, String ext){ 

    GenericExtFilter filter = new GenericExtFilter(ext); 
    File dir = new File(folder); 

    //list out all the file name with .txt extension 
    String[] list = dir.list(filter); 

    if (list.length == 0) return; 

    File fileDelete; 

    for (String file : list){ 
    String temp = new StringBuffer(FILE_DIR).append(File.separator).append(file).toString(); 
     fileDelete = new File(temp); 
     boolean isdeleted = fileDelete.delete(); 
     System.out.println("file : " + temp + " is deleted : " + isdeleted); 
    } 
    } 
public class GenericExtFilter implements FilenameFilter { 

     private String ext; 

     public GenericExtFilter(String ext) { 
     this.ext = ext;    
     } 

     public boolean accept(File dir, String name) { 
     return (name.endsWith(ext)); 
     } 
    } 

public void deleteFile2(String folder, String prefix){ 

    GenericExt filter = new GenericExt(prefix); 
    File dir = new File(folder); 

    //list out all the file name with .txt extension 
    String[] list = dir.list(filter); 

    if (list.length == 0) return; 

    File fileDelete; 

    for (String file : list){ 
    String temp = new StringBuffer(FILE_DIR).append(File.separator).append(file).toString(); 
     fileDelete = new File(temp); 
     boolean isdeleted = fileDelete.delete(); 
     System.out.println("file : " + temp + " is deleted : " + isdeleted); 
    } 
    } 
public class GenericExt implements FilenameFilter { 

     private String prefix; 

     public GenericExt(String prefix) { 
     this.prefix = prefix;    
     } 

     public boolean accept(File dir, String name) { 
     return (name.startsWith(prefix)); 
     } 
    } 

public void deleteFile3(String ID, String Chain) 
{ 
    File file = new File(ID+".pdb"); 
    if(file.delete()) 
    { 
    System.out.println(ID+"deleted"); 
    } 
    File file2 = new File(Chain+".pdb"); 
    if(file2.delete()) 
    { 
     System.out.println(Chain+"deleted"); 
    } 

} 


} 

此代碼是從另一個類中調用的。當繼續運行,因爲我的幾個代碼應該是..它不會刪除兩個pdb文件,特別是當我嘗試刪除我的特定文件擴展名的文件。我嘗試關閉它們,然後使用file.close(),然後再刪除它們時,整個文件名和擴展名是知道但又無濟於事。使用java刪除文件

String FILE_DIR = "F:/study"; 
    String FILE_TEXT_EXT1 = ".pdb"; 
    String FILE_TEXT_EXT2 = ".ali"; 
    String FILE_TEXT_EXT3 = ".txt"; 
    String FILE_TEXT_EXT4 = ".py"; 
    String FILE_TEXT_PREFIX1 = "target."; 
    String FILE_TEXT_PREFIX2 = "blastOutput."; 
// String FILE_TEXT_PREFIX3 = "1OPL."; 


new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT1); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT2); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT3); 
     new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT4); 
     new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX1); 
     new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX2); 
    // new FileChecker().deleteFile2(FILE_DIR,FILE_TEXT_PREFIX3); 
     BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_DIR+TemplatePDBID+".pdb", true)); 
    bw.close(); 
    BufferedWriter bw1 = new BufferedWriter(new FileWriter(FILE_DIR+Template+".pdb", true)); 
    bw1.close(); 
    new FileChecker().deleteFile3((FILE_DIR+TemplatePDBID), (FILE_DIR+Template)); 

這是在最後調用我FileChecker類的其他類的片段。

我粘貼了,我收到

file : F:/study\1OPL.pdb is deleted : false 
file : F:/study\1OPL_A.pdb is deleted : false 
file : F:/study\model.pdb is deleted : true 
file : F:/study\alignment.ali is deleted : true 
file : F:/study\tempAli.ali is deleted : true 
file : F:/study\1OPL.fasta.txt is deleted : true 
file : F:/study\ModelDescription.txt is deleted : true 
file : F:/study\ModellerOutput.txt is deleted : true 
file : F:/study\script.py is deleted : true 
file : F:/study\target.D00000001 is deleted : true 
file : F:/study\target.ini is deleted : true 
file : F:/study\target.rsr is deleted : true 
file : F:/study\target.sch is deleted : true 
file : F:/study\target.V99990001 is deleted : true 
file : F:/study\blastOutput.xml is deleted : true 

請幫我這個異常..我無法理解爲什麼它到底是跳在所有情況下的第一個兩個文件。

+0

是否有任何錯誤/異常? – 2013-03-16 18:31:05

+0

沒有..它只是在繼續運行時刪除這些文件..但是如果這個類被單獨調用..它刪除它們 – kuks 2013-03-16 18:33:58

+0

其他類寫入'F:/ study/Nirmiti/.....'你試試刪除在F:/ study – Ingo 2013-03-16 18:35:02

回答

0

另一個類不會寫入F:/study(或者至少您在編輯之前顯示的代碼沒有寫入)您期望的文件的位置,所以沒有。

此外,您可能需要使用構造

File(File, String) 

通過目錄列表中去的時候,即寫

fileDelete = new File(dir, file) 

,而不是在一個StringBuffer構建的名稱。

+0

Hi Ingo!謝謝你的幫助。但是所有文件都寫在同一個位置。當我第一次發佈這個問題時出現了輸入錯誤。所有刪除的文件都存儲在相同的位置。 – kuks 2013-03-16 18:42:49

+0

那麼,這些文件是否存在?是否有一些防止刪除的文件屬性? f.exists && f.isWritable應該是真實的。 – Ingo 2013-03-16 18:43:42

+0

是這些文件存在..我可以看到這些文件仍然在我的文件夾位置被刪除。並且是存在並且是可寫的。 – kuks 2013-03-16 18:47:53

0

鑑於您編寫的代碼以及其他文件被成功刪除的事實,只能得出結論,前2個文件存在權限或鎖定問題,無法刪除它們。你有沒有試圖從命令行中刪除它們?

+0

是的,我可以通過命令提示符將其刪除。 – kuks 2013-03-16 19:10:43