Search code examples
javaimagejavafx-8filechooser

JavaFX-8 FileChooser Image (backend copy-paste)


This is only the second question I have ever posted here on Stack Overflow, so hey guys! (please be gentle).

The next step in the project I'm doing involves files and the FileChooser library. Say I got the FileChooser to work, and that on a button click, the FileChooser opens and you can select the image you want.

Now say that the image comes from a flash drive plugged in to the computer. After taking the image, the filepath is stored into the database for later retrieval. But the problem, is that the filepath will be rendered useless when the flashdrive is plugged out.

Is there any way that to do a behind the scenes copy-paste of the image to the program's directory, so that I only need to take the filename, and append that to the default varchar value (proper directory minus filename) of the filepath column in the database?

I may be wording this wrong. This is in JavaFX-8 by the way. Any help would be appreciated.


Solution

  • Use Files.copy

    Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
    

    Note: source and destination are references of Path. Also, Files is located in the package java.nio.file