Soham Banerjee
Soham Banerjee

Reputation: 465

Can't delete files in java?

I am trying to delete all files in a folder:-

    import java.io.*;
public class AddService {   
    public static void main(String args[]){
        File folder=new File("inputs");
        File[] listOfFiles=folder.listFiles();
        for(File file:listOfFiles){
            if(file.delete())
                System.out.println("File deleted");
            else
                System.out.println("File not deleted");
        }
    }
}

I am getting response "File not deleted" and the file isn't getting deleted. What's wrong with my code?

Upvotes: 2

Views: 8949

Answers (7)

Alireza Saei
Alireza Saei

Reputation: 1

Try calling Garbage Collector before deleting file.

EDIT: In think that file is being used by other processes while deleting and because of this you can't delete the file you want.

Upvotes: 0

Suraj Waragade
Suraj Waragade

Reputation: 1

Java library delete function does not delete a directory if its not empty. Try recursion over that to delete all sub-directories and files.

OR use some external library like apache commons io

File file = new File("/your/path/here");
FileUtils.deleteDirectory(file);
import org.apache.commons.io.FileUtils;
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version><!-- check latest version-->
</dependency>

Upvotes: 0

dimo414
dimo414

Reputation: 48874

There are any number of reasons why a file cannot be deleted; it may not exist, it may be a non-empty directory, you may not have closed all resources using it, and your program may not have permission to do so, to name a few.

Unfortunately the File.delete() method provides very little information as to why; it's pretty much up to you to poke around and figure it out. But there's good news; you don't want to use File in the first place.

Java 7 introduced the new java.nio.file package which is a much more robust file access API. It provides the concept of an abstract Path and separates concrete operations into the Files class, in particular it provides Files.delete() which is documented to raise clear exceptions describing the reasons deletion might fail.

Use Path and Files; you'll be glad you did.

Upvotes: 7

Soham Banerjee
Soham Banerjee

Reputation: 465

I figured it out, I was using a FileReader to read contents of the file which I didn't close. Sorry for not providing the entire code

Upvotes: 0

v1shnu
v1shnu

Reputation: 2231

You have not given the full path for the folder. Also note to use forward slash.

try{
        File folder=new File("C:/xxxx/xxxx/xxxx/inputs");
        File[] listOfFiles=folder.listFiles();
        for(File file:listOfFiles){
            if(file.delete())
                System.out.println("File deleted");
            else
                System.out.println("File not deleted");
        }
}
catch(Exception e)
{
    System.out.println(e.printStackTrace());
}

Always use try-catch for code that contains methods which throws Exceptions.

Upvotes: -1

Sarah Aziziyan
Sarah Aziziyan

Reputation: 516

There is nothing wrong with your code except you should give the full path.

try this : File folder=new File("C:\\inputs");

instead of this line : File folder=new File("inputs");

Upvotes: -1

Shirish Hirekodi
Shirish Hirekodi

Reputation: 412

  1. Try giving the full path in this statement: "File folder=new File("inputs");"
  2. Use try-catch block and print the exception, if any

Upvotes: 0

Related Questions