简体   繁体   English

如何对BufferedReader使用JOptionPane字符串?

[英]How to use JOptionPane string for BufferedReader?

I am using a JOptionPane to ask the user for a file, which I then give to BufferedReader . 我正在使用JOptionPane向用户询问文件,然后将其提供给BufferedReader However, my code keeps throwing a FileNotFoundException . 但是,我的代码不断抛出FileNotFoundException Could someone please help me to understand why. 有人可以帮我理解为什么。

Here is my code... 这是我的代码...

edit = JOptionPane.showInputDialog("Enter a file to edit");
try { 
    BufferedReader fIn = new BufferedReader(new FileReader(edit));
    String in;
    try {
        while((in = fIn.readLine()) != null) {
            System.out.println(in);
        }
        fIn.close();
    }
    catch (IOException ex) {
        Logger.getLogger(WordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        fIn.close();
    }
    catch (IOException ex) {
        Logger.getLogger(WordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }

} 
catch (FileNotFoundException ex) {
    JOptionPane.showMessageDialog(null, "File does not exist");
    }
JOptionPane.showMessageDialog(null, "File does not exist");

Here you should do ex.printStackTrace() it will reveal exact problem. 在这里您应该执行ex.printStackTrace(),它将显示确切的问题。 Note that current directory of program will be one from where it is launched so any relative path should be in relation to that not the source directory.. 请注意,程序的当前目录将是从其启动的目录,因此任何相对路径都应与该目录而不是源目录相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM