Fah*_*kar 188 java file-io apache-commons-io
我想删除ABC目录中的所有文件.
当我尝试使用FileUtils.deleteDirectory(new File("C:/test/ABC/"));它时也删除文件夹ABC.
有一个单线解决方案,我可以删除目录内的文件,但不删除目录?
Red*_*ddy 332
FileUtils.cleanDirectory(directory);
Run Code Online (Sandbox Code Playgroud)
这个方法在同一个文件中可用.这也将递归删除其下的所有子文件夹和文件.
文档: org.apache.commons.io.FileUtils.cleanDirectory
Pet*_*rey 256
你的意思是?
for(File file: dir.listFiles())
if (!file.isDirectory())
file.delete();
Run Code Online (Sandbox Code Playgroud)
这只会删除文件,而不是目录.
Chr*_*isB 62
Peter Lawrey的答案很棒,因为它很简单,不依赖于任何特殊的东西,而且它是你应该这样做的.如果您需要删除子目录及其内容的东西,请使用递归:
void purgeDirectory(File dir) {
for (File file: dir.listFiles()) {
if (file.isDirectory())
purgeDirectory(file);
file.delete();
}
}
Run Code Online (Sandbox Code Playgroud)
要备用子目录及其内容(问题的一部分),修改如下:
void purgeDirectoryButKeepSubDirectories(File dir) {
for (File file: dir.listFiles()) {
if (!file.isDirectory())
file.delete();
}
}
Run Code Online (Sandbox Code Playgroud)
或者,因为您需要一个单行解决方案:
for (File file: dir.listFiles())
if (!file.isDirectory())
file.delete();
Run Code Online (Sandbox Code Playgroud)
使用外部库进行这样一个简单的任务并不是一个好主意,除非你无论如何都需要这个库,在这种情况下,最好使用现有的代码.您似乎仍在使用Apache库,因此请使用其FileUtils.cleanDirectory()方法.
Non*_*uit 30
这将仅删除ABC中的文件(子目录不受影响):
Arrays.stream(new File("C:/test/ABC/").listFiles()).forEach(File::delete);
Run Code Online (Sandbox Code Playgroud)
这将仅删除ABC(和子目录)中的文件:
Files.walk(Paths.get("C:/test/ABC/"))
.filter(Files::isRegularFile)
.map(Path::toFile)
.forEach(File::delete);
Run Code Online (Sandbox Code Playgroud)
^此版本需要处理IOException
Chr*_*oom 11
或者在Java 8中使用它:
try {
Files.newDirectoryStream( directory ).forEach( file -> {
try { Files.delete( file ); }
catch ( IOException e ) { throw new UncheckedIOException(e); }
} );
}
catch ( IOException e ) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
很遗憾,异常处理是如此笨重,否则它将是一个单行...
rm -rf性能比FileUtils.cleanDirectory.这不是一个单行解决方案,但经过广泛的基准测试后,我们发现 usingrm -rf比使用FileUtils.cleanDirectory.
当然,如果您有一个小或简单的目录,那没关系,但在我们的例子中,我们有多个 GB 和深度嵌套的子目录,使用 . 需要花费 10 多分钟,FileUtils.cleanDirectory而只需要 1 分钟rm -rf。
下面是我们的粗略 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;
}
Run Code Online (Sandbox Code Playgroud)
如果您正在处理大型或复杂的目录,则值得尝试。
小智 5
public class DeleteFile {
public static void main(String[] args) {
String path="D:\test";
File file = new File(path);
File[] files = file.listFiles();
for (File f:files)
{if (f.isFile() && f.exists)
{ f.delete();
system.out.println("successfully deleted");
}else{
system.out.println("cant delete a file due to open or error");
} } }}
Run Code Online (Sandbox Code Playgroud)