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.