2016-03-31 5 views
2

次のコードを使用してプロジェクトフォルダにファイルを移動しようとしています。コードはユーザーからファイルを取得していますが、新しい場所には移動しません。誰かが私を助けてくれますか?私には、事前に(プロジェクトフォルダ内の)別の場所にファイルを移動しようとしています

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

    } 
+0

あなたのプロジェクトの任意のリソースフォルダに移動しようとしていますか? –

+0

はい私は@ViswanathLekshmananです – AP6

+0

私はそれを助けてくれた皆様に感謝しました – AP6

答えて

1

私はそれを理解しました。ここで私は何をしました

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){} 
1

をありがとう、あなたがオブジェクト新しいファイルを作成しているが、ディスク上の必要な変更を行っていないように、それが見えます。ユーザーがselectedFileを選択したら、Files class' move() methodの使用を検討してください。

+0

ファイル移動クラスを試しましたが、何らかのエラーが発生しました@entpnerd – AP6

+0

{ Files.move(sr、ds、ATOMIC_MOVE); } catch(IOException e){ e.printStackTrace(); } @entpnerd – AP6

0

あなたのプロジェクトワークスペース内に作成される引用符のファイル間の値がなければ

File src = new File(""); 

を使用している場合。

関連する問題