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()关闭它们然后在知道整个文件名和扩展名时再次删除它们但是再次无效。
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 :(得分:0)
另一个类不会写入F:/study
(或者至少是你编辑之前显示的代码没有)你期望文件的位置,所以没有。
此外,您可能希望使用构造函数
File(File, String)
在浏览目录列表时,即写
fileDelete = new File(dir, file)
而不是在StringBuffer中构造名称。
答案 1 :(得分:0)
鉴于您编写的代码以及其他文件被成功删除的事实,我们只能得出结论,前两个文件存在权限或锁定问题,导致它们被删除。您是否尝试从命令行删除它们?