【问题标题】:How to get open file popup如何获得打开的文件弹出窗口
【发布时间】:2011-05-22 21:57:26
【问题描述】:

现在,我有一个设置的类路径,但我想弹出一个打开的文件,让用户选择要打开的文件。我已经尝试过 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";

我该怎么做呢?

【问题讨论】:

    标签: java file-io jfilechooser openfiledialog


    【解决方案1】:

    我认为问题在于File file的范围。

    尝试在 if 块之外声明 file

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

    【讨论】:

    • 我还需要使用声明 ReadFile files = new ReadFiles(***); 来读取文件***里面应该写什么?
    • @Irvilnius ReadFile 是什么?要读取文件,您可以使用FileInputStream。这个post 有一个例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多