首页 > 解决方案 > 如何从 JFileChooser 获取完整路径目录到字符串 Java

问题描述

所以我试图让我的程序选择一个带有 JFileChooser 的目录,然后使用该路径使用文件编写器创建一个文本文件,但我无法正确获取 JFileChooser 部分:(请帮助。

     public class parcourire0test {
     public static void main(String[] args) throws FileNotFoundException, IOException { 
      // TODO Auto-generated method stub           

       JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

         if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
          System.out.println("getSelectedFile() : " + chooser.getSelectedFile());

//这就是事情不起作用的地方

          String Stringpath=Stringvalueof(chooser.getSelectedFile());
          System.out.println("Stringpath: " + Stringpath);


        } else {
          System.out.println("No Selection ");
        }


        }

         private static String Stringvalueof(File fi) {
         // TODO Auto-generated method stub
         return null;
         }

         }

但我得到 Stringpath : null

标签: javastringswingpathjfilechooser

解决方案


推荐阅读