How Can I Tell if a File is Being Deleted in Eclipse?

By Barry Burd

Before deleting a file in Eclipse, the program asks the user if it’s okay to do the deletion. If the user gives one of the two expected answers (Y or N), the program proceeds according to the user’s wishes. But if the user enters any other letter (or any digit, punctuation symbol, or whatever), the program asks the user for another response.

image0.jpg

The user hems and haws for a while, first with the letter U, then the digit 8, and then with lowercase letters. Finally, the user enters Y, and the program deletes the importantData.txt file. If you compare the files on your hard drive (before and after the run of the program), you’ll see that the program trashes the file named importantData.txt.

If you use Eclipse, here’s how you can tell that a file is being deleted:

  1. Create a Java project.

  2. In the Package Explorer, select the project.

    Don’t select any of the project’s subfolders. (For example, don’t select the project’s src folder.) Instead, select the project’s root.

  3. In Eclipse’s main menu, choose File→New→File.

    Eclipse’s New File dialog box appears.

    In the New File dialog box, make sure that the name of your project’s root folder is in the box’s Enter Or Select The Parent Folder field.

  4. In the dialog box’s File Name field, type the name of your new file.

    Type importantData.txt.

  5. Click Finish.

  6. Observe that the file’s name appears in Eclipse’s Package Explorer.

    The name is project’s root directory.

    For this experiment, you don’t have to add any text to the file. The file exists only to be deleted.

  7. Run the program.

    When the program runs, type Y to delete the importantData.txt file.

    After running the program, you want to check to make sure that the program deleted the importantData.txt file.

  8. In the Package Explorer, select the project’s root (again, for good measure).

  9. In Eclipse’s main menu, choose File→Refresh.

    Eclipse takes another look at the project directory and lists the directory’s files in the Package Explorer’s tree. Assuming that the program did its job correctly, the file named importandData.txt no longer appears in the tree.

The statement

new File("importantData.txt").delete();

is tricky. At first glance, you seem to be creating a new file, only to delete that file in the same line of code! But in reality, the words new File create only a representation of a file inside your program.

To be more precise, the words new File create, inside your program, a representation of a disk file that may or may not already exist on your computer’s hard drive. So here’s what the new File statement really means:

“Let new File(“importantData.txt”)refer to a file named importantData.txt. If such a file exists, then delete it.”

Yes, the devil is in the details. But smiles are in the subtleties and nobility is in the nuance.