0

I've got a JComboBox filled with some java.io.File objects. By selecting one of these files in the ComboBox, I want to delete it either from the ComboBox and Filesystem.

Code snippet:

deleteButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            int dialogButton = JOptionPane.YES_NO_OPTION;
            int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure?", "Warning", dialogButton);

            if (dialogResult == JOptionPane.YES_OPTION)
            {
                Path path = Paths.get(mailingLists.getSelectedItem().toString());
                mailingLists.removeItem(mailingLists.getSelectedItem());

                try
                {
                    Files.delete(path);
                    JOptionPane.showMessageDialog(null, "File deleted!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException e1)
                {
                    JOptionPane.showMessageDialog(null, e1.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
                    e1.printStackTrace();
                }
            }
        }
    });

It gives this exception: java.nio.file.FileSystemException [...] file already in use this is because it's used by my application, then I thought first to remove it from the ComboBox and then delete it using Files.delete(path); but still have the exception.

What's wrong?

P.S.

Is the first time that I deal in this context so I guess if it's better to use File f = new File("path"); f.delete(); instead of Files.delete(path);.

EDIT: Provided more information about the JComboBox load.

Scratch:

    LinkedList<File> listFolder = new LinkedList<File>();
    listFolder.add(new File("mailinglists"));//<--- root folder

    File[] stuffInFolder = listFolder.get(0).listFiles();

    JComboBox<File> mailingLists = new JComboBox<File>(stuffInFolder);
Andrea Grimandi
  • 631
  • 2
  • 8
  • 32
  • Have you done anything other than deleting the files from the combobox? – Binkan Salaryman Jul 06 '15 at 10:19
  • *this is because it's used by my application* how have you used the file in your application? – Blip Jul 06 '15 at 10:23
  • @Binkan Salaryman: Nothing more than try to delete it; – Andrea Grimandi Jul 06 '15 at 10:42
  • @Blip: I't my supposition. I thought that the file was locked because it still was in the JCobmoBox. – Andrea Grimandi Jul 06 '15 at 10:42
  • Please post the complete stacktrace of the exception. – Blip Jul 06 '15 at 10:43
  • 1
    @AndreaGrimandi No, that's not how `File` works, in just an abstract representation of a file/path, until you actually attempt to perform an action against it (like delete it), there is no "physical" connection between them, none that would prevent you from deleting them anyway. For example, you could use `File#delete` to delete a file and then with the same instance of `File`, use `File#createNewFile` to create a new file with the same name and path... – MadProgrammer Jul 06 '15 at 10:53
  • Can you show use how you create your combo box? – MadProgrammer Jul 06 '15 at 10:54
  • `JComboBox mailingLists = new JComboBox(stuffInFolder)` where stuffInFolder is an array of File. – Andrea Grimandi Jul 06 '15 at 12:03
  • Did you tried as below suggested...http://stackoverflow.com/a/31243581/1129313 ? – Garry Jul 06 '15 at 13:49
  • Yes but still giving me the same exception. Now it's all converted using Strings (paths), no more java.io.File as you told me. Better check once more my code... – Andrea Grimandi Jul 06 '15 at 14:57

4 Answers4

1

Sounds like you need to close the file. When you open a file the OS will prevent a file from being deleted until the connection to the file has been closed.

dinamite
  • 231
  • 2
  • 5
  • So in my case I should get the File using `mailingLists.getSelectedItem()` and then close it or trying another way? – Andrea Grimandi Jul 06 '15 at 10:45
  • 1
    you need to get the stream that opened the file, then close it: [link] (http://stackoverflow.com/questions/10667734/java-file-open-a-file-and-write-to-it) – dinamite Jul 08 '15 at 00:50
1

I would recommend, instead of JComboBox filled with some java.io.File objects use file names with path as String. And when you have to delete the file create an Object of File using the path and delete it.

Garry
  • 4,493
  • 3
  • 28
  • 48
0

Use Java.io.File.delete()

try
{
   File f = new File(path);
   if(f.delete())
      JOptionPane.showMessageDialog(null, "File Deleted Succesfully!");
   else
      JOptionPane.showMessageDialog(null, "File couldn't be deleted!");
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • You should use an `if` statement to test what `f.delete()` returns, otherwise you have no idea if the operation completed successfully or not – MadProgrammer Jul 06 '15 at 11:20
0

Solved!

I was using a "bugged" external library developed by a workmate. Its goal was to read a .properties file. Once readed, the file was still opened.

Fixed and everything works well now.

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32