我正在尝试使用以下代码将文件移动到项目文件夹。该代码正在从用户那里获取文件,但它不会移动到新位置。有人可以帮帮我吗。预先感谢您
@FXML private void setNewPhotoButton(ActionEvent event){
Stage currentStage = (Stage) newPhotoButton.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose an image");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
File f = new File("photos/");
fileChooser.setInitialDirectory(f);
File selectedFile = fileChooser.showOpenDialog(currentStage);
if(selectedFile != null){
//System.out.println("C:/" + selectedFile.getPath());
//System.out.println("userfiles/"+UNAME+"/"+ANAME+"/");
File src = new File(selectedFile.getPath());
File dest = new File("userfiles/"+UNAME+"/"+ANAME+"/");
Path sr = src.toPath();
Path ds = new File(dest,src.getName()).toPath();
}
}
最佳答案
对我来说,您似乎正在创建新文件对象,但没有在磁盘上进行必要的更改。一旦用户选择了 selectedFile
,请考虑使用 Files
class' move() method .
关于java - 尝试将文件移动到不同位置(在项目文件夹中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36324184/