jfilechooser-将目录设置为文件中的路径 [英] jfilechooser - set directory to a path in a file

查看:68
本文介绍了jfilechooser-将目录设置为文件中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过这样的方式(使用commons-io)在JFilechooser中设置目录路径:

I am trying to set the directory path in JFilechooser through something like this(using commons-io ) :

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

I'm using getCanonicalPath() to get the path and write in the file path.txt

path = file.getCanonicalPath();

我不打算将所有代码都放在这里,但是我确定程序会在path.txt中写入和读取路径. 我没有收到任何错误,但是每次运行程序时,它总是在我的文档文件夹中打开JFilechooser.我在做什么错了?

I don't intend to put all my code here,but I'm sure that the program writes and reads the path in path.txt. I don't get any error,but everytime I run the program it always open JFilechooser in my documents folder.What i am doing wrong?

推荐答案

尝试直接在构造函数中传递当前目录:

Try to pass the current directory directly in the constructor:

filechooser = new JFileChooser(theDirectory);

这篇关于jfilechooser-将目录设置为文件中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆