0

I'm having some problems with file deletion in Java. I'm doing a simple CRUD application for my OOP class and we need to use files as database. I tried to create a function to erase the database (Delete all database files and save new empty ones), but, so far, I always receive the success message, but the files still there, untouched.

I have the file path set this way in my config.java file:

private String fileBooks = "books.dat";
private String fileUsers = "users.dat";
private String fileOperations = "operations.dat";

I have this function, responsible for file erase:

public void deleteFiles() {
        try {
            File fbooks = new File(config.getFileBooks());
            File fusers = new File(config.getFileUsers());
            File fop = new File(config.getFileOperations());
            if(fbooks.delete() && fusers.delete() && fop.delete()){
                JOptionPane.showMessageDialog(null, "Success!", 
                        "Database cleaning", JOptionPane.PLAIN_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null, "Error!", 
                        "Database cleaning", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I checked the inheritance, the controller, everything looks fine. What I'm doing wrong? Is it a problem with the path?

If is useful information I'm running a Ubuntu 14.04 LTS, using NetBeans to code.

Any help and code advice will be welcome.

EDIT:

I added the System.out.println(fbooks.getAbsoluteFile()) this was my output:

 /home/dotk/Dropbox/College/OOP/TP02/hw02/books.dat
 /home/dotk/Dropbox/College/OOP/TP02/hw02/users.dat
 /home/dotk/Dropbox/College/OOP/TP02/hw02/operations.dat

Do I need to change the path? I want the program to be OS independent, and it will be probably tested in a windows machine.

EDIT 2

I changed the file names, now they don't have the "./" but still not working. I verified that isn't any other open streams to the files, the only ones that run are in the start of the program to load the informations in ArrayLists, but they are closed after.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
inblank
  • 396
  • 1
  • 8
  • 26
  • 1
    Add `System.out.println(fbooks.getAbsoluteFile())`, and check that you're deleting the files you expect to delete. – JB Nizet Aug 23 '15 at 15:10
  • I did it. Look the output, Is there any way to solve this and keep OS independent? – inblank Aug 23 '15 at 15:22
  • 2
    You probably forgot to close an opened stream to these files (or another program still has one opened). See http://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handler-on-linux-if-the-pointed-file-gets-moved-de. Or the code recreates them after deleting them. – JB Nizet Aug 23 '15 at 15:27
  • Well, the idea of the functionality is to delete the current files and create new ones(this is made in the controller with a saveFiles() function). the deleteFiles functions is called, but I don't see where is the problem. Should I remove the file re-creation? – inblank Aug 23 '15 at 15:43
  • java.io.File is obsolete. You should be using [Path](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html) and [Files.delete](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#delete-java.nio.file.Path-) instead. Files.delete will throw an exception if the deletion fails—which is far more informative than a mere boolean return value. – VGR Aug 23 '15 at 16:27
  • 1
    @inblank I don't follow. If the program deletes files and then re-creates them, why are you surprised the files exist? – JB Nizet Aug 23 '15 at 16:57
  • The same file exists, theres no purge. – inblank Aug 23 '15 at 17:52

1 Answers1

-2

I found the error, the problem was in the order of the calls. I feel a little bit stupid, but now is fixed.

inblank
  • 396
  • 1
  • 8
  • 26
  • Rather than marking correct answer as "you found the solution". It would have been helpful to others if you explained what and where you fixed. – channae May 06 '21 at 10:27