无法通过Java删除目录 [英] not able to delete the directory through Java
本文介绍了无法通过Java删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我编写了从驱动器中删除目录的代码,但是当我检查File的删除功能时,它不会删除该文件。我写过这样的东西
In my application I have written the code to delete the directory from drive but when I inspect the delete function of File it doesn't delete the file. I have written some thing like this
//Code to delete the directory if it exists
File directory = new File("c:\\Report\\");
if(directory.exists())
directory.delete();
该目录尚未使用仍然无法删除目录
the directoryis not in used still it is not able to delete the directory
推荐答案
在Java中,目录删除只能用于空目录,这会产生如下方法:
in Java, directory deletion is possible only for empty directory, which leads to methods like the following :
/**
* Force deletion of directory
* @param path
* @return
*/
static public boolean deleteDirectory(File path) {
if (path.exists()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return (path.delete());
}
这个会删除你的文件夹,即使非空,也没有麻烦(当此目录被OS锁定时除外。
This one will delete your folder, even if non-empty, without troubles (excepted when this directory is locked by OS).
这篇关于无法通过Java删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文