简体   繁体   中英

How do I copy two separate files into two separate locations in Java?

mountainsListView.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent mouseEvent) {

                if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {

                    if (mouseEvent.getClickCount() == 2) {

                        FileChooser fileChooser = new FileChooser();

                        Window stage = null;
                        fileChooser.showOpenDialog(stage);
                        List<File> list = fileChooser.showOpenMultipleDialog(stage);
                        String path1 = "images/small/";
                        String path2 = "images/large/";
                        if (list != null) {
                                for (int i = 0; i < list.size(); i++) {
                                    File file = list.get(i);
                                    try {
                                        if (i == 0) {
                                            fileChooser.setTitle("Add Small Mountain File");
                                            Files.copy(file.toPath(), (new File(path1 + file.getName())).toPath(),
                                                    StandardCopyOption.REPLACE_EXISTING);
                                        } else {
                                            fileChooser.setTitle("Add Large Mountain File");
                                            Files.copy(file.toPath(), (new File(path2 + file.getName())).toPath(),
                                                    StandardCopyOption.REPLACE_EXISTING);
                                        }
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                };
                        }

                    }

                }
            }
        });

This is what I've got so far and what its doing is taking the second file I pick and putting it into the first directory (path1 or images/small/) when what I want to happen is the first file I pick goes into path1 and the second file I pick goes into path2.

So I figured it out, I had two separate filechoosers, one for single files and one for multiple so it was not filling the list the way I thought it was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM