如何打开文件弹出 [英] How to get open file popup

查看:199
本文介绍了如何打开文件弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有一个设置类路径,但我想打开一个打开的文件,用户选择打开哪个文件。我试过JFileChooser,但迄今为止还没有成功。这是我的代码:

pre $ 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();
}

//我不希望这是硬编码的:
String filePath =/Users/Bill/Desktop/hello.txt;

我应该如何去做这件事?

文件文件的范围。



尝试在if块之外声明文件

 文件file =空值; 
if(returnValue == JFileChooser.APPROVE_OPTION){
file = chooser.getSelectedFile();

if(file!= null)
{
String filePath = file.getPath();
}


Right now, I have a set class path, but I want to have an open file pop up and the user chooses which file to open. I've tried JFileChooser, but haven't been successful so far. Here's my code:

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";

How should I go about doing this?

解决方案

I think the problem is the scope of File file.

Try Declaring file outside the if-block.

 File file = null;
 if( returnValue == JFileChooser.APPROVE_OPTION ) {
        file = chooser.getSelectedFile() ;
 }
 if(file != null)
 {
      String filePath = file.getPath();
 } 

这篇关于如何打开文件弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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