如何在Java中删除包含其他文件夹的文件夹?

时间:2014-01-24 13:17:10

标签: java file file-io directory bluej

这是我尝试的代码:

import java.io.*;
public class file03 {
    public static void main(String[] args) {
        File f1 = new File("C:/tempo1/tempo");
        f1.mkdirs();
        File f2 = new File("C:/test");
        if(!f2.exists()) {
            f2.mkdir();
        }
        f1 = new File("C:/tempo1/kempo");
        f1.mkdirs();
        f1 = new File("C:/tempo1");
        String[] t = {};
        if(f1.exists()) {
            t = f1.list();
            System.out.println(t.length + " files found");
        }
        for(int i = 0; i < t.length; i++) {
            System.out.println(t[i]);
        }
        try {
            Thread.sleep(3000);
        }
        catch(Exception e) {}
        f2.delete();
        f2 = new File("C:/tempo1/test.txt");
        try {
            f2.createNewFile();
        }
        catch(Exception e) {}
        try {
            Thread.sleep(7000);
        }
        catch(Exception e) {}
        File f3 = new File("C:/tempo1/renametesting.txt");
        f2.renameTo(f3);
        try {
            Thread.sleep(5000);
        }
        catch(Exception e) {}
        f3 = new File("C:/tempo1");
        f3.delete();
    }
}

我注意到,当文件夹测试被删除时,文件夹tempo1不会被删除。是因为它包含其他文件夹和文件吗?如果是这样,我该如何删除它? 我正在使用BlueJ IDE。

4 个答案:

答案 0 :(得分:5)

在删除该文件夹的所有文件之前,无法删除文件夹。

首先删除该文件夹中的所有文件,然后删除该文件夹

这是删除文件夹的代码..

您需要传递 路径

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());
        }
    }

答案 1 :(得分:4)

public class DeleteFolder {
/**
 * Delete a folder and all content folder & files.
 * @param folder
 */
  public void rmdir(final File folder) {     
      if (folder.isDirectory()) {           //Check if folder file is a real folder
          File[] list = folder.listFiles(); //Storing all file name within array
          if (list != null) {               //Checking list value is null or not to check folder containts atlest one file
              for (int i = 0; i < list.length; i++) {    
                  File tmpF = list[i];
                  if (tmpF.isDirectory()) {   //if folder  found within folder remove that folder using recursive method
                      rmdir(tmpF);
                  }
                  tmpF.delete();             //else delete file
              }
          }
          if (!folder.delete()) {            //if not able to delete folder print message
            System.out.println("can't delete folder : " + folder);
          }
      }
  }
}

答案 2 :(得分:2)

要删除包含文件的文件夹,不需要循环或递归搜索。您可以直接使用:

FileUtils.deleteDirectory(<File object of directory>);

此功能将删除文件夹及其中的所有文件

答案 3 :(得分:0)