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
請幫我這個異常..我無法理解爲什麼它到底是跳在所有情況下的第一個兩個文件。
是否有任何錯誤/異常? – 2013-03-16 18:31:05
沒有..它只是在繼續運行時刪除這些文件..但是如果這個類被單獨調用..它刪除它們 – kuks 2013-03-16 18:33:58
其他類寫入'F:/ study/Nirmiti/.....'你試試刪除在F:/ study – Ingo 2013-03-16 18:35:02