我想删除ABC目录下的所有文件。

当我尝试FileUtils.deleteDirectory(新文件(“C:/test/ABC/”));它还会删除文件夹ABC。

是否有一个一行解决方案,我可以删除目录内的文件,但不是目录?


当前回答

rm -rf比FileUtils.cleanDirectory的性能要好得多。

不是一行程序解决方案,但经过大量的基准测试后,我们发现使用rm -rf比使用FileUtils.cleanDirectory快几倍。

当然,如果您有一个小的或简单的目录,这没有关系,但在我们的例子中,我们有多个gb和深嵌套的子目录,使用FileUtils将花费10分钟以上的时间。使用rm -rf只需要1分钟。

下面是我们粗略的Java实现:

// Delete directory given and all subdirectories and files (i.e. recursively).
//
static public boolean clearDirectory( File file ) throws IOException, InterruptedException {

    if ( file.exists() ) {

        String deleteCommand = "rm -rf " + file.getAbsolutePath();
        Runtime runtime = Runtime.getRuntime();

        Process process = runtime.exec( deleteCommand );
        process.waitFor();

        file.mkdirs(); // Since we only want to clear the directory and not delete it, we need to re-create the directory.

        return true;
    }

    return false;

}

如果您正在处理大型或复杂的目录,值得一试。

其他回答

rm -rf比FileUtils.cleanDirectory的性能要好得多。

不是一行程序解决方案,但经过大量的基准测试后,我们发现使用rm -rf比使用FileUtils.cleanDirectory快几倍。

当然,如果您有一个小的或简单的目录,这没有关系,但在我们的例子中,我们有多个gb和深嵌套的子目录,使用FileUtils将花费10分钟以上的时间。使用rm -rf只需要1分钟。

下面是我们粗略的Java实现:

// Delete directory given and all subdirectories and files (i.e. recursively).
//
static public boolean clearDirectory( File file ) throws IOException, InterruptedException {

    if ( file.exists() ) {

        String deleteCommand = "rm -rf " + file.getAbsolutePath();
        Runtime runtime = Runtime.getRuntime();

        Process process = runtime.exec( deleteCommand );
        process.waitFor();

        file.mkdirs(); // Since we only want to clear the directory and not delete it, we need to re-create the directory.

        return true;
    }

    return false;

}

如果您正在处理大型或复杂的目录,值得一试。

import org.apache.commons.io.FileUtils;

FileUtils.cleanDirectory(directory); 

在同一文件中有此方法可用。这也会递归地删除它们下面的所有子文件夹和文件。

文档:org.apache.commons.io.FileUtils.cleanDirectory

你是说像?

for(File file: dir.listFiles()) 
    if (!file.isDirectory()) 
        file.delete();

这将只删除文件,而不是目录。

另一个Java 8 Stream解决方案删除文件夹的所有内容,包括子目录,但不删除文件夹本身。

用法:

Path folder = Paths.get("/tmp/folder");
CleanFolder.clean(folder);

代码是:

public interface CleanFolder {
    static void clean(Path folder) throws IOException {

        Function<Path, Stream<Path>> walk = p -> {
            try { return Files.walk(p);
        } catch (IOException e) {
            return Stream.empty();
        }};

        Consumer<Path> delete = p -> {
            try {
                Files.delete(p);
            } catch (IOException e) {
            }
        };

        Files.list(folder)
            .flatMap(walk)
            .sorted(Comparator.reverseOrder())
            .forEach(delete);
    }
}

每个流解决方案都涉及文件的问题。walk或Files.delete这些方法会抛出IOException,这在流中处理起来很麻烦。

我试图创造一个更简洁的解决方案。

或者在Java 8中使用这个:

try {
  Files.newDirectoryStream( directory ).forEach( file -> {
    try { Files.delete( file ); }
    catch ( IOException e ) { throw new UncheckedIOException(e); }
  } );
}
catch ( IOException e ) {
  e.printStackTrace();
}

很遗憾异常处理是如此庞大,否则它将是一行程序…