-1

I have a small java program that create & delete employee data. I store employee data as .txt (text file). Everything is fine, but it fails to delete the text file.

SIMPLE CLASS TO DELETE text file

    public void removeFile(String ID)
    {

    File file = new File("file"+ID+".txt");
      if(file.exists())
       {
         if(file.delete());
         {
           System.out.println("\nEmployee has been removed Successfully");
         }
       }
      else
       {
            System.out.println("\nEmployee does not exists :( ");
       }
     }

I am getting message "Employee has been removed Successfully" but when I check the root folder, I can still see the file is still there (not deleted).

I tried and checked out different codes online /google, all shows no problem in delete () function.

  • 1
    Have you verified that the absolute path of `new File("file"+ID+".txt")` actually matches the path of the file you expect to be deleted? – Mark Rotteveel Dec 03 '22 at 09:09
  • @MarkRotteveel . I was trying to check absolute path by printing out it just after the "new file" line of above snippet. System.out.println(file.getAbsolutePath());. Nothing else , now files are getting deleted . No idea what happened , printing out absolute path made code work !!!!! thank you for your question. – thenoobdev133332323 Dec 03 '22 at 09:18
  • try printing the absolute path before deleting – m3ow Dec 03 '22 at 09:21
  • 1
    *"No idea what happened"* - My guess is that you ran the program in a different directory. Read about how your operating system handles relative pathnames. – Stephen C Dec 03 '22 at 09:29
  • @StephenC literally I added the print then ran again, did not change any directory or moved any file. – thenoobdev133332323 Dec 03 '22 at 09:52
  • Well ... literally (just) adding a print wouldn't change anything. So something else must have happened. Whether you realize or not. – Stephen C Dec 03 '22 at 10:21
  • 1
    Well I now see what you added. And it was NOT just a print statement. The problem is caused by that extraneous semicolon. See the answer below. – Stephen C Dec 03 '22 at 10:48
  • semicolon is still there, but file is getting deleted, see the comment below- I don't know what changed, but I am only aware of only 1 change that being I added a print statement. BTW who closed the question, semicolon was not the problem. I ran it several times . – thenoobdev133332323 Dec 03 '22 at 10:53

1 Answers1

2

I'm not sure why it didn't delete the file (my guess would be you have a file of the same name in the project root, but you are wanting to delete one stored elsewhere) but there is a syntax error on the if statement:

if (file.delete());

You need to remove the semi-colon at the end of that line because that is terminating the if clause. The following System.out line is not conditional on file.delete() being true or false. It will be printed regardless.

See what outcome you get when removing the semi-colon.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Andrew B
  • 69
  • 3
  • Thank you @Andrew B , but I am running my code successfully without removing semi colon. I mentioned above what i did (print our file location just below "new file". Did not changed location, file path. Its working well and deleting the correct files. I am clue less why printing out path did it. (Seems like somebody closed the question claiming it was semicolon. But it is not) – thenoobdev133332323 Dec 03 '22 at 10:51
  • 1
    Your code will run fine with the semi-colon but it will **always** output "Employee has been deleted successfully" even when the delete fails. If you remove the semi-colon, it will only print that on success. – Andrew B Dec 03 '22 at 12:39