我正在尝试通过类似以下方式(使用commons-io)设置JFileChooser中的目录路径:
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File theDirectory = new File(fileContents);
filechooser = new JFileChooser();
fileChooser.setCurrentDirectory(theDirectory);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
我正在使用getCanonicalPath()获取路径并将其写入文件path.txt
path = file.getCanonicalPath();
我不打算在这里放置全部的代码,但我确定程序会在 path.txt 中写入和读取路径。 我没有得到任何错误,但每次运行程序时它总是在我的文档文件夹中打开 JFilechooser。我做错了什么?
theDirectory
并检查它是否存在吗?System.out.println(theDirectory.getCanonicalPath() + " exists: " + theDirectory.exists());
如果文件不存在,则文件选择器将默认为“我的文档”文件夹。 - wolfcastle