0

Whats wrong with my code ? with reference with this question

the folder is not being deleted.

    File f = new File(directory+"\\OOO");
    if (f.exists())
    {            
        System.out.println(f);

        FileUtils.delete(f);

        f.delete();
    //  f.mkdir();
    }
    else
    {
        System.out.println("created");

        //f.mkdir();
    }

Method defined

Andrea
  • 6,032
  • 2
  • 28
  • 55
Moudiz
  • 7,211
  • 22
  • 78
  • 156

3 Answers3

3

FileUtils does not contain any method named delete();
You should use deleteDirectory() to recursively delete directory.
Or you can also use deleteQuitely() to suppress any exceptions while deletion.

Moudiz
  • 7,211
  • 22
  • 78
  • 156
Shubham Chaurasia
  • 2,472
  • 2
  • 15
  • 22
1

FileUtils does not contain any method named delete(), so you should use deleteDirectory() instead. Also, please ensure you have imported the correct Java file utility class from Apache Commons IO. If it is not the case, your need to

  • Download Apache Commons IO as a JAR file, link it to the classpath
  • Or declare a dependency the Apache Commons IO in your Maven, or other dependency management system.

Here's the code:

import org.apache.commons.io.FileUtils;

public class YourClass
{

    public void yourMethod() throws IOException
    {
        File f = new File(directory+"\\OOO");
        if (f.exists())
        {            
            FileUtils.deleteDirectory(f);
        }
        ...
    }
}
Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
0

This may help

   try {
        FileUtils.deleteDirectory(new File("path");
        //path Example - C:\\myfolder\\foldertodelete
    } catch (IOException e) {

    }
Guna
  • 1
  • 4