使用 Java 删除所有带有扩展名的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13663843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete all files with an extension using Java
提问by tucuxi
I'm (relatively) new to Java and I'm trying to implement a .jar that runs a list of commands that in Windows XP's command prompt it would be:
我(相对)是 Java 新手,我正在尝试实现一个 .jar 文件,该文件运行一系列命令,在 Windows XP 的命令提示符下,它将是:
cd\
cd myfolder
del *.lck /s
My (failed) attempt:
我的(失败)尝试:
// Lists all files in folder
File folder = new File(dir);
File fList[] = folder.listFiles();
// Searchs .lck
for (int i = 0; i < fList.length; i++) {
String pes = fList.get(i);
if (pes.contains(".lck") == true) {
// and deletes
boolean success = (new File(fList.get(i)).delete());
}
}
I screwed somewhere around that "get(i)", but I think I'm pretty close to my goal now.
我在某个地方搞砸了“get(i)”,但我认为我现在已经很接近我的目标了。
I ask for your help and thank you very much in advance!
我请求您的帮助,并在此先感谢您!
EDIT
编辑
Alright! Many thanks, everybody. With the 3 suggested modifications I ended up with:
好吧!非常感谢大家。通过 3 个建议的修改,我最终得到了:
// Lists all files in folder
File folder = new File(dir);
File fList[] = folder.listFiles();
// Searchs .lck
for (int i = 0; i < fList.length; i++) {
String pes = fList[i];
if (pes.endsWith(".lck")) {
// and deletes
boolean success = (new File(fList[i]).delete());
}
}
And now it works!
现在它起作用了!
采纳答案by Rohit Jain
fList.get(i)
should be fList[i]
as fList
is an array, and it returns a File
reference not a String
.
fList.get(i)
应fList[i]
作为fList
是一个数组,并且它返回一个File
参考不是一个String
。
Change: -
改变: -
String pes = fList.get(i);
to: -
到: -
File pes = fList[i];
And then change if (pes.contains(".lck") == true)
to if (pes.getName().contains(".lck"))
然后if (pes.contains(".lck") == true)
改为if (pes.getName().contains(".lck"))
In fact, since you are checking for the extension
, you should use endsWith
method rather than contains
method. And yes, you don't need to compare your boolean
value with ==
. So just use this condition: -
事实上,由于您正在检查extension
,您应该使用endsWith
方法而不是contains
方法。是的,您不需要将您的boolean
价值与==
. 所以只需使用这个条件: -
if (pes.getName().endsWith(".lck")) {
boolean success = (new File(fList.get(i)).delete());
}
回答by tucuxi
for (File f : folder.listFiles()) {
if (f.getName().endsWith(".lck")) {
f.delete(); // may fail mysteriously - returns boolean you may want to check
}
}
回答by Johnny Willer
Java 8 approach
Java 8 方法
Arrays.stream(yourDir.listFiles((f, p) -> p.endsWith("YOUR_FILE_EXTENSION"))).forEach(File::delete);
回答by Yasmeen Azhar
Final Code which works :)
最终代码有效:)
File folder = new File(dir);
File fList[] = folder.listFiles();
for (File f : fList) {
if (f.getName().endsWith(".png")) {
f.delete();
}}
回答by Yogendra Singh
You are using Collection
method get
on an Array
. Use Array Index
notation as below:
您正在使用Collection
的方法get
上Array
。使用Array Index
符号如下:
File pes = fList[i];
Also better to use endsWith()String method over the file name as:
最好 在文件名上使用endsWith()String 方法:
if (pes.getName().endsWith(".lck")){
...
}