【发布时间】:2010-10-29 05:08:12
【问题描述】:
我需要做一些家务。我不小心将我的类路径设置为与我的代码库相同,并且所有类都与我的代码一起放置。我需要编写一个快速的 java 程序来单独选择 .class 和 .class 类型的所有文件并立即删除。有没有人做过与此相关的事情?
【问题讨论】:
我需要做一些家务。我不小心将我的类路径设置为与我的代码库相同,并且所有类都与我的代码一起放置。我需要编写一个快速的 java 程序来单独选择 .class 和 .class 类型的所有文件并立即删除。有没有人做过与此相关的事情?
【问题讨论】:
你为什么不使用 shell 来做到这一点,比如:
Linux:
find . -name *.class -print -exec rm {} \;
窗户:
for /r %f in (*.class) do del %f
【讨论】:
find . -name "*.class" -exec rm '{}' \;
【讨论】:
这可能有效。未经测试。其他人的那些 find/for 命令看起来也很有希望,但以防万一你在 OS/390 大型机上,这里是 Java。 ;-)
import java.io.File;
import java.io.IOException;
public class RemoveClass {
public static void main(String[] args) throws Exception {
File f = new File(".");
deleteRecursive(f);
}
public static void deleteRecursive(File f) throws IOException {
if (f.isDirectory()) {
for (File file : f.listFiles()) {
deleteRecursive(file);
}
} else if (f.isFile() && f.getName().endsWith(".class")) {
String path = f.getCanonicalPath();
// f.delete();
System.out.println("Uncomment line above to delete: [" + path + "]");
}
}
}
【讨论】:
QSHELL 和 find :)