Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a folder containing other folders in Java? [duplicate]

Here is a code I tried:

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

what I noticed was that while the folder test gets deleted, the folder tempo1 doesn't get deleted. Is it because it contains other folders and files? If so, how can I delete it? I am using BlueJ IDE.

like image 299
user3177527 Avatar asked Dec 19 '22 18:12

user3177527


2 Answers

A folder can not be deleted until all files of that folder are deleted.

First delete all files from that folder then delete that folder

This is code for deleting a folder..

You need to pass the path of the folder only

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());
        }
    }
like image 79
AJ. Avatar answered Dec 29 '22 10:12

AJ.


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);
          }
      }
  }
}
like image 42
Anupam Maiti Avatar answered Dec 29 '22 12:12

Anupam Maiti