gpt4 book ai didi

java - 创建目录后无法删除

转载 作者:行者123 更新时间:2023-12-01 19:18:51 29 4
gpt4 key购买 nike

我正在尝试按顺序创建然后删除一个目录。但是,删除该目录似乎不起作用。

有人知道为什么吗?是不是Java中文件系统没有刷新导致的?

public boolean createDirectory(File file) {
// Delete Directory if alreday exists
if (file.exists()) {
deleteDirectory(file);
}
boolean status = file.mkdirs();
if (status) {
System.out.println(" Successfull of creating Directory " + file.getPath());
}
return status;
}

public boolean deleteDirectory(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
File delFile = new File(dir, children[i]);
if (!delFile.exists()) {
System.out.println("Cannot find directory to delete" + delFile.getPath());
return false;
}
boolean success = deleteDirectory(delFile);
System.out.println(delFile + ": success? " + success);
if (!success) {
System.out.println("failure during delete directory" + delFile.getPath());
return false;
}
}
// The directory is now empty so now it can be smoked
return dir.delete();
}
}

最佳答案

如果它在 Windows 上运行,那么问题通常是,如果任何进程“使用”某个目录,Windows 不会删除该目录,即打开该目录(或其子目录之一)中的文件,或者将该目录(或其子目录之一)作为其当前工作目录。

关于java - 创建目录后无法删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5353347/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com