Shane Fitzgerald
Shane Fitzgerald

Reputation: 97

Unable to delete a File in Java

For whatever reason I am having an issue with File.delete() and File.renameto(File). For example:

private void doWork(){
    File inputFile = new File("resources/custom/inputFile.txt");
    System.out.println(inputFile.delete());
}

This returns false for me and does not delete the file.

I don't have this file opened or in use anywhere else and I don't understand why I can't delete it. Has anyone else encountered this or have any insight into the problem?

Upvotes: 0

Views: 666

Answers (2)

salahy
salahy

Reputation: 1197

try this

private void doWork(){
    File inputFile = new File("resources\\custom\\inputFile.txt");
    System.out.println(inputFile.delete());
}

also it will help if you add try and catch

Upvotes: -1

Daniel Earwicker
Daniel Earwicker

Reputation: 116654

Try adding a line:

System.out.println(inputFile.getCanonicalPath());

This will print the actual path (starting from the root) and maybe reveal that it isn't the path you're expecting.

Upvotes: 3

Related Questions