无法使用Java删除文件夹
我试图删除一个只有文件但没有子文件夹的文件夹,但没有成功 代码: 输出:无法使用Java删除文件夹,java,file,file-io,java-io,Java,File,File Io,Java Io,我试图删除一个只有文件但没有子文件夹的文件夹,但没有成功 代码: 输出: 代码不会删除任何文件夹或文件。为什么会这样?您可能会因为一些原因而导致删除失败;文件可能被文件系统锁定,您可能缺乏权限,或者可能被其他进程打开,等等 如果您使用的是Java7或更高版本,那么可以使用javax.nio.*API;它比java.io.File类更可靠、更一致: Path fp = file.toPath(); Files.delete(fp); 如果要捕获可能的异常,请执行以下操作: try { F
代码不会删除任何文件夹或文件。为什么会这样?您可能会因为一些原因而导致删除失败;文件可能被文件系统锁定,您可能缺乏权限,或者可能被其他进程打开,等等 如果您使用的是Java7或更高版本,那么可以使用
javax.nio.*
API;它比java.io.File
类更可靠、更一致:
Path fp = file.toPath();
Files.delete(fp);
如果要捕获可能的异常,请执行以下操作:
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
查看以获取有关Java 7中IO的更多信息,请尝试此操作。资料来源:
由于没有检查delete()的返回值,因此生成的输出没有意义。删除失败的原因可能有很多:
- 该文件是非空目录
- 该文件由其他用户打开(在某些平台上)
- 您没有删除该文件的权限
public class DeleteDirTest {
public static void main(String[] args) throws IOException {
DeleteDirTest test = new DeleteDirTest();
boolean result = test.deleteDir(new File("D:/test"));
System.out.println(result);
}
public boolean deleteDir(File file) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(file, children[i]));
if (!success) {
return false;
}
}
}
return file.delete();
}
}
公共类测试{
公共静态void main(字符串[]args)引发IOException{
DeleteDirTest test=新的DeleteDirTest();
布尔结果=test.deleteDir(新文件(“D:/test”);
系统输出打印项次(结果);
}
公共布尔deleteDir(文件){
if(file.isDirectory()){
String[]children=file.list();
for(int i=0;i
您可以首先检查delete()
的返回值(布尔值)以查看它是否运行良好。也许你没有这样做的权利。delete方法返回一个布尔值,请检查该值…当我使用apache commons的FileUtils.deleteDirectory()时,文件夹已成功删除。这就解决了问题。我肯定会检查返回的布尔值,看看前面的代码有什么问题,我在问题中展示了一个非常糟糕的源代码。由于此代码不检查任何错误,因此它完全能够不删除文件,并且在这种情况下会产生完全误导性的输出。当我使用apache commons的FileUtils.deleteDirectory()时,该文件夹已成功删除。这就解决了问题。我肯定会检查返回的布尔值,看看我在问题中显示的前一个代码有什么问题只是检查不会告诉你有什么问题,但它会告诉你有什么问题,以及在哪个文件或目录。然后您可以查看该文件或目录。
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
import java.io.File;
import java.io.IOException;
public class DeleteDirectoryExample
{
private static final String SRC_FOLDER = "C:\\mkyong-new";
public static void main(String[] args)
{
File directory = new File(SRC_FOLDER);
//make sure directory exists
if(!directory.exists()){
System.out.println("Directory does not exist.");
System.exit(0);
}else{
try{
delete(directory);
}catch(IOException e){
e.printStackTrace();
System.exit(0);
}
}
System.out.println("Done");
}
public static void delete(File file)
throws IOException{
if(file.isDirectory()){
//directory is empty, then delete it
if(file.list().length==0){
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}else{
//list all the directory contents
String files[] = file.list();
for (String temp : files) {
//construct the file structure
File fileDelete = new File(file, temp);
//recursive delete
delete(fileDelete);
}
//check the directory again, if empty then delete it
if(file.list().length==0){
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}
}
}else{
//if file, then delete it
file.delete();
System.out.println("File is deleted : " + file.getAbsolutePath());
}
}
}
public class DeleteDirTest {
public static void main(String[] args) throws IOException {
DeleteDirTest test = new DeleteDirTest();
boolean result = test.deleteDir(new File("D:/test"));
System.out.println(result);
}
public boolean deleteDir(File file) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(file, children[i]));
if (!success) {
return false;
}
}
}
return file.delete();
}
}