javafxfilechooser

How to save a file using FileChooser in JavaFX


How to save a file using FileChooser from JavaFX, here's my sample:

public static void clickDownloadButton(String filename,Stage window){
   File file = new File(filename);
   FileChooser fileChooser = new FileChooser();
   fileChooser.setTitle("Save file");
   fileChooser.showSaveDialog(window);
}

Solution

  • Use java.nio.file.Files -

    File dest = fileChooser.showSaveDialog(window);
    if (dest != null) {
        try {
            Files.copy(file.toPath(), dest.toPath());
        } catch (IOException ex) {
            // handle exception...
        }
    }