How to delete a folder with files using Java

后端 未结 28 3026
北荒
北荒 2020-11-28 05:36

I want to create and delete a directory using Java, but it isn\'t working.

File index = new File(\"/home/Work/Indexer1\");
if (!index.exists()) {
    index.m         


        
相关标签:
28条回答
  • 2020-11-28 05:44

    Just a one-liner.

    import org.apache.commons.io.FileUtils;
    
    FileUtils.deleteDirectory(new File(destination));
    

    Documentation here

    0 讨论(0)
  • 2020-11-28 05:45

    Using Apache Commons-IO, it is following one-liner:

    import org.apache.commons.io.FileUtils;
    
    FileUtils.forceDelete(new File(destination));
    

    This is (slightly) more performant than FileUtils.deleteDirectory.

    0 讨论(0)
  • 2020-11-28 05:45

    As mentioned, Java isn't able to delete a folder that contains files, so first delete the files and then the folder.

    Here's a simple example to do this:

    import org.apache.commons.io.FileUtils;
    
    
    
    // First, remove files from into the folder 
    FileUtils.cleanDirectory(folder/path);
    
    // Then, remove the folder
    FileUtils.deleteDirectory(folder/path);
    

    Or:

    FileUtils.forceDelete(new File(destination));
    
    0 讨论(0)
  • 2020-11-28 05:47

    directry cannot simply delete if it has the files so you may need to delete the files inside first and then directory

    public class DeleteFileFolder {
    
    public DeleteFileFolder(String path) {
    
        File file = new File(path);
        if(file.exists())
        {
            do{
                delete(file);
            }while(file.exists());
        }else
        {
            System.out.println("File or Folder not found : "+path);
        }
    
    }
    private void delete(File file)
    {
        if(file.isDirectory())
        {
            String fileList[] = file.list();
            if(fileList.length == 0)
            {
                System.out.println("Deleting Directory : "+file.getPath());
                file.delete();
            }else
            {
                int size = fileList.length;
                for(int i = 0 ; i < size ; i++)
                {
                    String fileName = fileList[i];
                    System.out.println("File path : "+file.getPath()+" and name :"+fileName);
                    String fullPath = file.getPath()+"/"+fileName;
                    File fileOrFolder = new File(fullPath);
                    System.out.println("Full Path :"+fileOrFolder.getPath());
                    delete(fileOrFolder);
                }
            }
        }else
        {
            System.out.println("Deleting file : "+file.getPath());
            file.delete();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:48

    In this

    index.delete();
    
                if (!index.exists())
                   {
                       index.mkdir();
                   }
    

    you are calling

     if (!index.exists())
                       {
                           index.mkdir();
                       }
    

    after

    index.delete();
    

    This means that you are creating the file again after deleting File.delete() returns a boolean value.So if you want to check then do System.out.println(index.delete()); if you get true then this means that file is deleted

    File index = new File("/home/Work/Indexer1");
        if (!index.exists())
           {
                 index.mkdir();
           }
        else{
                System.out.println(index.delete());//If you get true then file is deleted
    
    
    
    
                if (!index.exists())
                   {
                       index.mkdir();// here you are creating again after deleting the file
                   }
    
    
    
    
            }
    

    from the comments given below,the updated answer is like this

    File f=new File("full_path");//full path like c:/home/ri
        if(f.exists())
        {
            f.delete();
        }
        else
        {
            try {
                //f.createNewFile();//this will create a file
                f.mkdir();//this create a folder
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    0 讨论(0)
  • 2020-11-28 05:48

    Here is a simple way to do it :

    public void deleteDirectory(String directoryPath)  {
          new Thread(new Runnable() {
              public void run() {
                 for(String e: new File(directoryPath).list()) {
                     if(new File(e).isDirectory()) 
                         deleteDirectory(e);
                     else 
                         new File(e).delete();
                 }
              }
          }).start();
      }
    
    0 讨论(0)
提交回复
热议问题