0

I am new in JavaFx, I wonder how to copy a file already selected by Filechooser to my project folder.

public void ButtonAction(ActionEvent event) {
    FileChooser fc = new FileChooser();
    fc.setTitle("attach a file");
    File selectedFile = fc.showOpenDialog(null);

    if (selectedFile != null) {
        file1.setText("selectionned file : " + selectedFile.getAbsolutePath());

        //the code to copy the selected file goes here//

    } else{
        file1.setText("no file attached");
    }
André Kool
  • 4,880
  • 12
  • 34
  • 44
Yassine EL
  • 23
  • 1
  • 3

4 Answers4

2

problem resolved thanks anyways.

Path from = Paths.get(selectedFile.toURI());
        Path to = Paths.get("pathdest\\file.exe");
        CopyOption[] options = new CopyOption[]{
                StandardCopyOption.REPLACE_EXISTING,
                StandardCopyOption.COPY_ATTRIBUTES
        };
        Files.copy(from, to, options);
Yassine EL
  • 23
  • 1
  • 3
1

You can use the Files class to copy files, e.g.:

Files.copy(selectedFile.toPath, targetDirPath);
Puce
  • 37,247
  • 13
  • 80
  • 152
1

The Solution by Applemelon works just that I didn't call the toFile() method in Files.copy(from.toFile(), to.toFile()); which gives a cannot resolve method error instead Files.copy(from, to) worked for me.



    private Path to;
    private Path from;
    private File selectedFile;

    private void handleFileLocationSearcher() throws IOException {
        FileChooser fc = new FileChooser();
        fc.setTitle("Attach a file");
        selectedFile = fc.showOpenDialog(null);

        if (selectedFile != null) {
            from = Paths.get(selectedFile.toURI());
            to = Paths.get("Your destination path" + selectedFile.getName());
            // Files.copy(from.toFile(), to.toFile()); //gives a 'cannot resolve method error 
            Files.copy(from, to);
        }
    }


Cozy
  • 311
  • 5
  • 16
0

To make it a bit easier for anyone looking to copy the actual code of this method and having a bit of trouble with the above code (since some of it simply doesn't work):

private Path to;
private Path from;
private File selectedFile;

private void handleFileLocationSearcher() throws IOException {
    FileChooser fc = new FileChooser();
    fc.setTitle("Attach a file");
    selectedFile = fc.showOpenDialog(null);

    if (selectedFile != null) {
        from = Paths.get(selectedFile.toURI());
        to = Paths.get("Your destination path" + selectedFile.getName());
        Files.copy(from.toFile(), to.toFile());
    }
}

You can use selectedFile.toString() or selectedFile.getName() to add it into a Text Field or just in general get the path or name of the file you're attempting to retrieve through File Chooser.

You can also use Files.copy(from.toFile(), to.toFile()); somewhere else in your application if you want it to happen on another button press because the variables can be used anywhere in the class. If you don't need to do this though, just create the local variables in the method.

kalabalik
  • 3,792
  • 2
  • 21
  • 50