Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将所选文件复制到项目目录_Java_Javafx_Filechooser_File Copying - Fatal编程技术网

Java 将所选文件复制到项目目录

Java 将所选文件复制到项目目录,java,javafx,filechooser,file-copying,Java,Javafx,Filechooser,File Copying,我是JavaFx新手,我想知道如何将Filechooser已经选择的文件复制到我的项目文件夹中 public void ButtonAction(ActionEvent event) { FileChooser fc = new FileChooser(); fc.setTitle("attach a file"); File selectedFile = fc.showOpenDialog(null); if (selectedFile != null) {

我是JavaFx新手,我想知道如何将Filechooser已经选择的文件复制到我的项目文件夹中

public void ButtonAction(ActionEvent event) {
    FileChooser fc = new FileChooser();
    fc.setTitle("attach a file");
    File selectedFile = fc.showOpenDialog(null);

    if (selectedFile != null) {
        file1.setText("selectionned file : " + selectedFile.getAbsolutePath());

        //the code to copy the selected file goes here//

    } else{
        file1.setText("no file attached");
    }
您可以使用该类复制文件,例如:

Files.copy(selectedFile.toPath, targetDirPath);
您可以使用该类复制文件,例如:

Files.copy(selectedFile.toPath, targetDirPath);

问题解决了,谢谢

Path from = Paths.get(selectedFile.toURI());
        Path to = Paths.get("pathdest\\file.exe");
        CopyOption[] options = new CopyOption[]{
                StandardCopyOption.REPLACE_EXISTING,
                StandardCopyOption.COPY_ATTRIBUTES
        };
        Files.copy(from, to, options);

问题解决了,谢谢

Path from = Paths.get(selectedFile.toURI());
        Path to = Paths.get("pathdest\\file.exe");
        CopyOption[] options = new CopyOption[]{
                StandardCopyOption.REPLACE_EXISTING,
                StandardCopyOption.COPY_ATTRIBUTES
        };
        Files.copy(from, to, options);

对于那些希望复制此方法的实际代码并且在上面的代码中遇到一些问题的人(因为其中一些代码根本不起作用),要使其更容易一些:

您可以使用
selectedFile.toString()
selectedFile.getName()
将其添加到文本字段中,或者通常通过文件选择器获取要检索的文件的路径或名称


您还可以使用
Files.copy(从.toFile()复制到.toFile())
如果希望它发生在另一个按钮上,请在应用程序中的其他位置按,因为变量可以在类中的任何位置使用。但是,如果不需要这样做,只需在方法中创建局部变量。

对于那些希望复制此方法的实际代码并且在上面的代码中遇到一些问题的人(因为有些代码根本不起作用),可以更容易地使用此方法:

您可以使用
selectedFile.toString()
selectedFile.getName()
将其添加到文本字段中,或者通常通过文件选择器获取要检索的文件的路径或名称

您还可以使用
Files.copy(从.toFile()复制到.toFile())
如果希望它发生在另一个按钮上,请在应用程序中的其他位置按,因为变量可以在类中的任何位置使用。如果不需要这样做,只需在方法中创建局部变量。

解决方案有效,因为我没有在中调用
toFile()
方法
Files.copy(从.toFile()复制到.toFile())
给出了一个无法解决方法错误,而不是
文件。复制(从,到)
对我有效



    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()); //gives a 'cannot resolve method error 
            Files.copy(from, to);
        }
    }

解决方案的作用在于我没有在中调用
toFile()
方法
Files.copy(从.toFile()复制到.toFile())
给出了一个无法解决方法错误,而不是
文件。复制(从,到)
对我有效



    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()); //gives a 'cannot resolve method error 
            Files.copy(from, to);
        }
    }