读取文件时未报告的异常FileNotFoundException

时间:2013-09-07 04:14:04

标签: java user-interface

我试图读取使用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(未显示)中。我将按钮放在类动画consructor中,并从那里调用LoadFile的actionPerformed。

4 个答案:

答案 0 :(得分: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) {}
}

当你完成阅读时,你也没有关闭阅读器。黄金法则,如果你打开它,你必须关闭它;)

答案 1 :(得分:0)

您的BufferedWriter初始化声明也必须位于trycatch块中。这是因为当您创建新的FileWriter对象时,它会抛出IOException,必须在trycatch块下处理。 This是文档。

答案 2 :(得分: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块之外,这促使它告诉你捕获或抛出它

答案 3 :(得分: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;
     }