Java file deletion [closed]

I have this as my code:

      public static void ResetAccount() 
      { 
          System.gc();
          try  
          {    
              File f= new File("User.json");
              f.exists();
              if(f.delete())
              {  
                  System.out.println(f.getName() + " deleted");
              }  
              else  
              {
                  System.out.println("failed");  
              }  
          }  
          catch(Exception e)  
          {  
              e.printStackTrace();  
          }   
          UI.LoginScreen();
      }
}

I want to delete the file 'User.json'. How can I do this? I tried several ways already but i just cant get it deleted.


Solution 1:

This code is working fine. When you are deleting the file, I would recommend putting the complete path where you expect the file to be. In this case though, your User.json file needs to be present inside your project and not inside your package as shared in the image.
enter image description here

Using your shared code, it deletes the file.