Java 正在尝试将文件移动到其他位置(在项目文件夹中)
我正在尝试使用以下代码将文件移动到项目文件夹。代码从用户处获取文件,但不会移动到新位置。谁能帮帮我吗。先谢谢你Java 正在尝试将文件移动到其他位置(在项目文件夹中),java,file-io,javafx,fxml,Java,File Io,Javafx,Fxml,我正在尝试使用以下代码将文件移动到项目文件夹。代码从用户处获取文件,但不会移动到新位置。谁能帮帮我吗。先谢谢你 @FXML private void setNewPhotoButton(ActionEvent event){ Stage currentStage = (Stage) newPhotoButton.getScene().getWindow(); FileChooser fileChooser = new FileChooser();
@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();
}
}
在我看来,您似乎正在创建新的文件对象,但没有对磁盘进行必要的更改。一旦用户选择了<代码>选择文件,如果使用,请考虑使用.< /P>< P>
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){}
您是否正在尝试移动到项目的任何资源文件夹中?是的,我是@ViswanathLekshmanani。我找到了它。谢谢大家的帮助。我尝试了文件移动类,但我遇到了某种错误@entpnerdtry{Files.move(sr,ds,ATOMIC_move);}catch(IOException e){e.printStackTrace();}@entpnerd