强制删除文件夹中的所有文件

强制删除文件夹中的所有文件

本文介绍了强制删除文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用特定的一段代码来删除文件夹中的文件,但是这样做很成问题,因为我可能忘了关闭一个或两个InputStream。我所拥有的代码太大了,我无法看到所有没有关闭的输入流。是否有删除文件的方式是否有打开的InputStream?

这是我一直在使用的代码块;

  File fin = new File(C:/ ABC Statements final /); 
File [] finlist = fin.listFiles();
for(int n = 0; n if(finlist [n] .isFile()){
System.gc();
Thread.sleep(2000);
finlist [n] .delete();
}
}

我编辑了代码。这个版本的工作原理。

解决方案
在提供的代码块中没有InputStream实例。

要不写大量未经测试的IO代码,请查看项目。特别是在类中,用于文件删除操作。



您的代码可能如下所示:

  File fin = new File( C:/ ABC声明final /); (文件file:fin.listFiles()){
FileDeleteStrategy.FORCE.delete(file);


}


I have been using a specific piece of code to delete files from a folder but it is proving very problematic because maybe I forgot to close an InputStream or two. The code I have is so big that I am not be able to see all the Inputstreams that I have not closed. Is there a way of deleting files whether there is an open InputStream or not?

This is the piece of the code that I have been using;

File fin = new File("C:/ABC Statements final/");
    File[] finlist = fin.listFiles();
    for (int n = 0; n < finlist.length; n++) {
        if (finlist[n].isFile()) {
        System.gc();
        Thread.sleep(2000);
            finlist[n].delete();
        }
    }

I have edited the code. This version works.

解决方案

There is no InputStream instances in the provided chunk of code.

To not write lots of untested IO code, please take a look at the apache.commons.io project. Especially at the FileDeleteStrategy class, for file deletion operations.

Your code might look like that:

File fin = new File("C:/ABC Statements final/");

for (File file : fin.listFiles()) {
    FileDeleteStrategy.FORCE.delete(file);
}

这篇关于强制删除文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:21