现在,我有一个设置的类路径,但我想要弹出一个打开的文件,并且用户选择要打开的文件。我尝试过 JFileChooser,但到目前为止还没有成功。这是我的代码:
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog( null ) ;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
File file = chooser.getSelectedFile() ;
}
// I don't want this to be hard-coded:
String filePath = "/Users/Bill/Desktop/hello.txt";
我该怎么做呢?
我认为问题在于范围File file
.
尝试声明file
在 if 块之外。
File file = null;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
file = chooser.getSelectedFile() ;
}
if(file != null)
{
String filePath = file.getPath();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)