当前位置: 技术问答>java相关
一个文件没发现的异常处理(FileNotFoundException),请大家指教啊!!!
来源: 互联网 发布时间:2015-11-09
本文导语: BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));这条语句造成的错误。 为什么这条语句放在 public void actionPerformed(ActionEvent e)方法中就会报错,报错如下: "MainFrame.java": Error #: 360 : unreported exception: j...
BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));这条语句造成的错误。
为什么这条语句放在 public void actionPerformed(ActionEvent e)方法中就会报错,报错如下:
"MainFrame.java": Error #: 360 : unreported exception: java.io.FileNotFoundException; must be caught or declared to be thrown at line 128, column 27
而放在private void jbInit() throws Exception 这个方法中却能正常运行。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
/**
*
*
*
*
* @author zhq
* @version 1.0
*/
public class MainFrame extends JFrame implements ActionListener{
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileOpen=new JMenuItem();
JMenuItem jMenuFileSave=new JMenuItem();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButtonOpen= new JButton();
JButton jButtonSave = new JButton();
JButton jButtonHelp= new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
image1 = new ImageIcon(zhq.MainFrame.class.getResource("openFile.gif"));
image2 = new ImageIcon(zhq.MainFrame.class.getResource("closeFile.gif"));
image3 = new ImageIcon(zhq.MainFrame.class.getResource("help.gif"));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Main Frame");
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileOpen.setText("Open");
jMenuFileOpen.addActionListener(this);
jMenuFileSave.setText("Save");
jMenuFileSave.addActionListener(this);
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(this);
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(this);
jButtonOpen.setIcon(image1);
jButtonOpen.setToolTipText("Open File");
jButtonOpen.addActionListener(this);
jButtonSave.setIcon(image2);
jButtonSave.setToolTipText("Close File");
jButtonSave.addActionListener(this);
jButtonHelp.setIcon(image3);
jButtonHelp.setToolTipText("Help");
jTextArea1.setText("jTextArea1");
jToolBar.add(jButtonOpen);
jToolBar.add(jButtonSave);
jToolBar.add(jButtonHelp);
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jTextArea1, BorderLayout.CENTER);
BufferedReader br=new BufferedReader(new FileReader ("D:\odbcconf.log"));《不出错》
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
dispose();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jMenuFileExit) {
dispose();
}
else if(e.getSource()==jMenuHelpAbout){
MainFrame_AboutBox dlg=new MainFrame_AboutBox(this);
Dimension dlgSize=dlg.getPreferredSize();
Dimension frmSize=getSize();
Point loc=getLocation();
dlg.setLocation((frmSize.width-dlgSize.width)/2+loc.x,(frmSize.height-dlgSize.height)/2+loc.y);
dlg.setModal(true);
dlg.show();
}
else if((e.getSource()==jButtonOpen)||(e.getSource()==jMenuFileOpen)) {
FileDialog openFileDialog=new FileDialog(this,"Open",0);
//openFileDialog.setFilenameFilter(filter);
openFileDialog.setDirectory(".");
openFileDialog.show();
String file=openFileDialog.getFile();
if (file==null)
return;
jTextArea1.append(file.toString());
String directory=openFileDialog.getDirectory();
File f=new File(directory,file);
if (f.exists()){
BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));《出错》
String strf=f.toString();
jTextArea1.append(strf);
FileReader fs=new FileReader(f);
}
}
else if((e.getSource()==jButtonSave)||(e.getSource()==jMenuFileSave)){
FileDialog openFileDialog=new FileDialog(this,"SaveAs",1);
openFileDialog.show();
}
}
}
为什么这条语句放在 public void actionPerformed(ActionEvent e)方法中就会报错,报错如下:
"MainFrame.java": Error #: 360 : unreported exception: java.io.FileNotFoundException; must be caught or declared to be thrown at line 128, column 27
而放在private void jbInit() throws Exception 这个方法中却能正常运行。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author zhq
* @version 1.0
*/
public class MainFrame extends JFrame implements ActionListener{
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileOpen=new JMenuItem();
JMenuItem jMenuFileSave=new JMenuItem();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButtonOpen= new JButton();
JButton jButtonSave = new JButton();
JButton jButtonHelp= new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
image1 = new ImageIcon(zhq.MainFrame.class.getResource("openFile.gif"));
image2 = new ImageIcon(zhq.MainFrame.class.getResource("closeFile.gif"));
image3 = new ImageIcon(zhq.MainFrame.class.getResource("help.gif"));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Main Frame");
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileOpen.setText("Open");
jMenuFileOpen.addActionListener(this);
jMenuFileSave.setText("Save");
jMenuFileSave.addActionListener(this);
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(this);
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(this);
jButtonOpen.setIcon(image1);
jButtonOpen.setToolTipText("Open File");
jButtonOpen.addActionListener(this);
jButtonSave.setIcon(image2);
jButtonSave.setToolTipText("Close File");
jButtonSave.addActionListener(this);
jButtonHelp.setIcon(image3);
jButtonHelp.setToolTipText("Help");
jTextArea1.setText("jTextArea1");
jToolBar.add(jButtonOpen);
jToolBar.add(jButtonSave);
jToolBar.add(jButtonHelp);
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jTextArea1, BorderLayout.CENTER);
BufferedReader br=new BufferedReader(new FileReader ("D:\odbcconf.log"));《不出错》
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
dispose();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jMenuFileExit) {
dispose();
}
else if(e.getSource()==jMenuHelpAbout){
MainFrame_AboutBox dlg=new MainFrame_AboutBox(this);
Dimension dlgSize=dlg.getPreferredSize();
Dimension frmSize=getSize();
Point loc=getLocation();
dlg.setLocation((frmSize.width-dlgSize.width)/2+loc.x,(frmSize.height-dlgSize.height)/2+loc.y);
dlg.setModal(true);
dlg.show();
}
else if((e.getSource()==jButtonOpen)||(e.getSource()==jMenuFileOpen)) {
FileDialog openFileDialog=new FileDialog(this,"Open",0);
//openFileDialog.setFilenameFilter(filter);
openFileDialog.setDirectory(".");
openFileDialog.show();
String file=openFileDialog.getFile();
if (file==null)
return;
jTextArea1.append(file.toString());
String directory=openFileDialog.getDirectory();
File f=new File(directory,file);
if (f.exists()){
BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));《出错》
String strf=f.toString();
jTextArea1.append(strf);
FileReader fs=new FileReader(f);
}
}
else if((e.getSource()==jButtonSave)||(e.getSource()==jMenuFileSave)){
FileDialog openFileDialog=new FileDialog(this,"SaveAs",1);
openFileDialog.show();
}
}
}
|
兄弟,你的private void jbInit() throws Exception抛出了异常,而你的方法public void actionPerformed(ActionEvent e)并没有抛出异常,所以编译器要求你捕获异常啊。
|
同意楼上,
java觉得你的文件可能没有或打开会出错,
所以安全第一,让你加上try-catch以便出错的时候处理
你可以在
BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));
这句话上套一个try-catch就行了!
java觉得你的文件可能没有或打开会出错,
所以安全第一,让你加上try-catch以便出错的时候处理
你可以在
BufferedReader br=new BufferedReader(new FileReader("D:\odbcconf.log"));
这句话上套一个try-catch就行了!
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。