试图将文件移动到不同的位置(在项目文件夹中)

trying to move file to different location(in project folder)

我正在尝试使用以下代码将文件移动到项目文件夹。代码正在从用户那里获取文件,但它并没有移动到新位置。有人可以帮帮我吗。提前谢谢你

@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();
        }

    }

对我来说,您似乎正在创建新文件 objects,但并未在磁盘上进行必要的更改。用户选择 selectedFile 后,请考虑使用 Files class' move() method.

如果你使用

File src = new File("");

将在您的项目工作区中创建引号文件之间没有值。

我搞定了,谢谢大家的帮助。这是我所做的

Path movefrom = FileSystems.getDefault().getPath(selectedFile.getPath());
            Path target = FileSystems.getDefault().getPath("userfiles/"+UNAME+"/"+ANAME+"/"+selectedFile.getName());
            Path targetDir = FileSystems.getDefault().getPath("userfiles/"+UNAME+"/"+ANAME);
            try{
                Files.move(movefrom,target,StandardCopyOption.ATOMIC_MOVE);
            }catch (IOException e){}