返回String变量(JAVA)中的完整目录路径

时间:2017-10-13 17:43:19

标签: java file

我正在尝试使用JFileChooser让用户选择一个文件。然后绝对目录需要在一个字符串变量中,所以我可以在以后的代码中使用它。到目前为止,我还没有在网上找到任何解决方案/提示。到目前为止,我有......

FileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.getCurrentDirectory();
chooser.setDialogTitle("Select Image to detect facial presence");
chooser.setFileFilter(filter);

int returnVal = chooser.showOpenDialog(null);

2 个答案:

答案 0 :(得分:0)

基于Get the path of a directory using JFileChooser指示的@RobertMoskal's comment

FileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.getCurrentDirectory();
chooser.setDialogTitle("Select Image to detect facial presence");
chooser.setFileFilter(filter);

int returnVal = chooser.showOpenDialog(null);

//ADDITIONAL CODE
String filePath;
if(returnVal == JFileChooser.APPROVE_OPTION) {
    filePath = chooser.getSelectedFile().getAbsolutePath()); //<<<<<
}

答案 1 :(得分:0)

 JFileChooser chooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
          "JPG & GIF Images", "jpg", "gif");
      chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      chooser.getCurrentDirectory();
      chooser.setDialogTitle("Select Image to detect facial presence");
      chooser.setFileFilter(filter);
      String temp;
      int returnVal = chooser.showOpenDialog(null);
      temp = chooser.getSelectedFile().getAbsolutePath();