0

我试图读取使用 JFileChooser 打开的文件,但出现以下错误

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

BufferedReader br = new BufferedReader(new FileReader(file));

这是代码片段:

    class LoadFile implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            File f = new File("/home/ralf/Documents");
            String line = null;

            chooser.setCurrentDirectory(f);

            int returnVal = chooser.showOpenDialog(null);

            if(returnVal == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();              
                BufferedReader br = new BufferedReader(new FileReader(file));

               try {
                while((line = br.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (FileNotFoundException fnfe) {
                    JOptionPane.showMessageDialog(null, fnfe.getMessage());
                    return;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }               
            } else {

            }
        }
    }

注意:类 LoadFile 可在扩展 JFrame 的类 Animation(未显示)中找到。我将按钮放在类 Animation 构造函数中,并从那里调用 LoadFile 的 actionPerformed。

4

4 回答 4

1

基本上,你试图BufferedReader在块外创建一个try-catch,因为actionPerformed不能抛出任何类型的异常,编译器抱怨

BufferedReader br = null;
try {        
    br = new BufferedReader(new FileReader(file));

    while((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (FileNotFoundException fnfe) {
    JOptionPane.showMessageDialog(null, fnfe.getMessage());
    return;
} catch (IOException ex) {
     ex.printStackTrace();
} finally {
    try {
        // Best effort to close the reader...
        br.close();
    } catch (Exception exp) {}
}

完成后,您也不会关闭阅读器。黄金法则,如果你打开它,你必须关闭它;)

于 2013-09-07T04:24:35.563 回答
0

您的BufferedWriter初始化语句也必须在 atrycatch块中。这是因为当您创建一个新FileWriter对象时,它会抛出一个IOException必须在 atrycatch块下处理的事实。是文档。

于 2013-09-07T04:16:37.377 回答
0

尝试使用:

class LoadFile implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        File f = new File("/home/ralf/Documents");
        String line = null;

        chooser.setCurrentDirectory(f);

        int returnVal = chooser.showOpenDialog(null);

        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();      
            try {        
            BufferedReader br = new BufferedReader(new FileReader(file));

            while((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (FileNotFoundException fnfe) {
                JOptionPane.showMessageDialog(null, fnfe.getMessage());
                return;
            } catch (IOException ex) {
                ex.printStackTrace();
            }               
        } else {

        }
    }
}

您的BufferedReader br = new BufferedReader(new FileReader(file));语句位于 try-catch 块之外,这促使它告诉您要么捕获它,要么抛出它

于 2013-09-07T04:16:58.243 回答
0

将 BufferedReader 行移动到 try-catch 块中。

try {
     BufferedReader br = new BufferedReader(new FileReader(file));
     while((line = br.readLine()) != null) {
         System.out.println(line);
     }
     } catch (FileNotFoundException fnfe) {
        JOptionPane.showMessageDialog(null, fnfe.getMessage());
        return;
     }
于 2013-09-07T04:16:58.617 回答