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

时间:2016-03-31 04:29:07

标签: java file-io javafx fxml

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

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

    }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我明白了,谢谢大家的帮助。这就是我做的事情

scrollView?.contentSize.height = textView!.contentSize.height

答案 2 :(得分:0)

如果您使用

File src = new File("");

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

相关问题