使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:32:39  来源:igfitidea点击:

Delete all files with an extension using Java

javafile

提问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 fListis an array, and it returns a Filereference 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 endsWithmethod rather than containsmethod. And yes, you don't need to compare your booleanvalue 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 Collectionmethod geton an Array. Use Array Indexnotation as below:

您正在使用Collection的方法getArray。使用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")){
      ...
   }