rogerthat
rogerthat

Reputation: 1815

Loop only deletes first file

I'm having a problem getting all the selected files deleted. What I'm trying to do is after clicking "Add" whatever files are selected are moved to a new folder and are deleted in their previous folder. One file works fine. It deletes and moves the file. But more than one and only the first gets deleted. My loop is recognizing each file just not deleting them. I'm posting the actionevent. If more code is needed let me know. I've indicated where the problem is, so I think, so you don't have to search the code.

public void actionPerformed(ActionEvent e) {
    int returnValue = 0;
    int option = 0;
    File[] selectedFiles = new File[0];

    if (e.getActionCommand().equals("CLOSE")) System.exit(0);

    else if (e.getActionCommand().equals("ADD")) {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        returnValue = chooser.showOpenDialog(this);         

        if (returnValue == JFileChooser.APPROVE_OPTION) {

            File[] file = chooser.getSelectedFiles();                   

            try {
                FileInputStream fstream = null;
                FileOutputStream ostream = null;

                for (int i = 0; i < file.length; i++) {

                   fstream = new FileInputStream(file[i]);
                           ostream = new      
                                       FileOutputStream(file[i].getName());

                    Path path = Paths.get(file[i].getPath());
                    byte[] fileArray;
                    fileArray = Files.readAllBytes(path);

                    listModel.add(0, file[i].getName());
                    selectedFilesList.setModel(listModel);
                                  //ostream.write(fileArray, 0, fileArray.length);

                }

                fstream.close();
                //ostream.close();

                try {

                    for(int i = 0; i < file.length; i++) {
//**----------------------->>>PROBLEM**                     
                    Files.delete(Paths.get(file[i].getPath()));
                    System.out.println(file[i].getName());

                    }


                } catch (NoSuchFileException x) {}
        System.err.format("%s: no such" + " file or directory%n")
                } catch (DirectoryNotEmptyException x) {
                    System.err.format("%s not empty%n");
                } catch (IOException x) {
                    // File permission problems are caught here.
                    System.err.println(x);
                } catch (Exception err) {
            }
        }

Upvotes: 2

Views: 867

Answers (2)

David Waters
David Waters

Reputation: 12028

This could be caused by you failing to close your file streams in the first loop.

            for (int i = 0; i < file.length; i++) {
               fstream = new FileInputStream(file[i]);
                       ostream = new      
                                   FileOutputStream(file[i].getName()); // This is never closed

                Path path = Paths.get(file[i].getPath());
                byte[] fileArray;
                fileArray = Files.readAllBytes(path);

                listModel.add(0, file[i].getName());
                selectedFilesList.setModel(listModel);
                              //ostream.write(fileArray, 0, fileArray.length);

            }

            fstream.close();  // Only the last input stream is closed 

should be more like

            for (int i = 0; i < file.length; i++) {
              try {
                 fstream = new FileInputStream(file[i]);
                 ostream = new      
                      FileOutputStream(file[i].getName());

                  Path path = Paths.get(file[i].getPath());
                  byte[] fileArray;
                  fileArray = Files.readAllBytes(path);

                  listModel.add(0, file[i].getName());
                  selectedFilesList.setModel(listModel);
                  //ostream.write(fileArray, 0, fileArray.length);
               } finally {
                 fstream.close();
                 ostream.close();
               }
            }

Closing the same number of files you open.

This could be causing your problem by holding a lock on all but one of your files which would prevent deletion.

Also your catch exception block (last statement) does nothing with the error.

Upvotes: 4

Axel
Axel

Reputation: 14159

Don't move files like that!

  1. If you are on Java 7, have at look at this page instead.

  2. For older versions, use oldFile.renameTo(newFile).

EDIT: To understand why your code is not working, use a debugger. I would think your deletion loop is left because of an exception.

Upvotes: 3

Related Questions