0

I have a file in java that I want to delete but I can't. Here is my code.

public static File fileSave1 = new File("saves/save1.txt");

fileSave1.delete();
System.out.println("File Deleted.");
Main.wait(2000);

As you can see I have tried to use .delete but it doesn't work. I have done research and this is the only way I have seen to delete a file. I believe if I change the file type by removing static it may work but then it would break all of my methods. Any help is appreciated, thanks!

if (fileSave1.exists()) returns true 
  • 2
    How, specifically, is it not working? Is there an error message, is it just seeming to do nothing? Also what's the return value of `File#delete`? – CollinD Jul 24 '18 at 15:15
  • 1
    Did you try providing an absolute path rather than a relative path ? You can also check what `fileSave1.exists()` returns . – Arnaud Jul 24 '18 at 15:15
  • 2
    `delete()` returns a `boolean`. Check if it is `true`. https://docs.oracle.com/javase/10/docs/api/java/io/File.html#delete() –  Jul 24 '18 at 15:15
  • Check if the file [exists](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#exists--), it's likely a path issue. If not, make sure you have write permission on this file (and execution permision on its directory). – SystemGlitch Jul 24 '18 at 15:16
  • Try Providing absolute file (complete path) name instead of relative path. It also can't delete it if its in use. – Abhishek Tiwari Jul 24 '18 at 17:32

0 Answers0