0

My Java Windows application keeps data for a particular problem in a file aProblem.db that is in a directory (folder) of the same name, e.g., Documents\MyApplication\aProblem\aProblem.db. I would like the user to be able to delete aProblem without leaving the application. So the aProblem.db file has to be deleted, and IF aProblem directory is left empty it too should be deleted. Because FileChooser does not allow file deletion (at least from my reading of the documentation) I built my own dialog with a select box, and Ok button, and a Cancel button. The select box is populated by searching MyApplication for all directories x containing a file x.db.

The code below is the deletion part, after the user has made a selection and clicked OK. It works except for when the directory is open in Windows Explorer, when an exception is thrown when directory delete is attempted. I know it's because Explorer has a lock on it, but I'd like to avoid asking the user to close it in the Explorer and try again. Is this possible?

NOTE:I use JFileChooser.APPROVE_OPTION, but DO NOT use JFileShooser.

           int resp = dialog.showOpenDialog(); 
        if(resp == JFileChooser.APPROVE_OPTION){
            strConcoursFolderPath = dialog.getSelectedFolderFullPath();
            int response = yesNoDialog("Do you really want to delete " + strConcoursFolderPath + "?");
            if(response == JOptionPane.YES_OPTION) {
               File dir = new File(strConcoursFolderPath);
               boolean dirExist = dir.exists() && dir.isDirectory();
               if(dirExist) {
                   String strDbFile = dialog.getSelectedFolderName() + ".db";
                   File db = new File(dir + "\\" + strDbFile);
                   if(db.exists()) db.delete();
                   File[] listOfFiles = dir.listFiles();
                   if(listOfFiles.length == 0) dir.delete();
               }  
            }

        } else {
            //okDialog("Cancel button clicked");
        }
Ed S
  • 239
  • 5
  • 21
  • 1
    Possible duplicate of [Deleting locked files with Java?](http://stackoverflow.com/questions/2885406/deleting-locked-files-with-java) – boxed__l Feb 08 '17 at 01:02

2 Answers2

1

I wrote up some pseudo code that you can use as a template.

    File directory = new File(/* current directory you are viewing files in JFileChooser  */);

    if (directory.exists()) {

        //List our content within this directory.
        File[] files = directory.listFiles();

        //Select file to delete.

        //If contents is empty it'll delete the directory.
        if (files.length == 1) {
            directory.delete();
        }
    }

This should do the trick based on what you described in the post. We check if the file content is 1 simply because upon the deletion of a file, you'll never been in a scenario where you're deleting a file & contents of the directory is equal to zero. Because of that we check if this is the last file in the directory then delete the directory upon deleting the last file.

Simplemind
  • 41
  • 3
  • Thanks, but I couldn't understand how this applied to my problem, probably because I didn't explain it very well. But, as you see the "problem" didn't exist anyway! – Ed S Feb 09 '17 at 03:37
  • @EdS My apologies, I guess I didn't interpret it all correctly. – Simplemind Feb 09 '17 at 18:07
0

The answer "it never really happened." There was an unrelated bug and when fixed the problem could not be replicated. IOW, my code works just fine whether the director is open in Explorer or not.

Sorry

Ed

Ed S
  • 239
  • 5
  • 21