我写了一个类,打开文件,弹出选择的文件,思路是想把选择的文件内容显示在fileTextArea中,可是写了这样个函数,找到文件,点击打开,提示“读取发生错误”,那位大师帮忙看看,谢谢了public void approveSelection(){
filename=fileChooser.getSelectedFile().getName();
System.out.println(filename);
fileTextArea.setText(null);
try {
String str;
BufferedReader br=new BufferedReader(new FileReader(filename));
while(true){
str=br.readLine();
if(str==null){
break;
}
//System.out.println(str);
fileTextArea.append(str+'\n');
System.out.println(fileTextArea);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(dialogFrame.getContentPane(), "读取发生错误");
}
dialogFrame.dispose();//关闭对话框
filename=fileChooser.getSelectedFile().getName();
System.out.println(filename);
fileTextArea.setText(null);
try {
String str;
BufferedReader br=new BufferedReader(new FileReader(filename));
while(true){
str=br.readLine();
if(str==null){
break;
}
//System.out.println(str);
fileTextArea.append(str+'\n');
System.out.println(fileTextArea);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(dialogFrame.getContentPane(), "读取发生错误");
}
dialogFrame.dispose();//关闭对话框
e.printStackTrace();
JOptionPane.showMessageDialog(dialogFrame.getContentPane(), "读取发生错误");
}
把你的异常打印出来,看看是什么异常
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at funonto.MainFrame$FileChooser.approveSelection(MainFrame.java:289)
at javax.swing.plaf.basic.BasicFileChooserUI$ApproveSelectionAction.actionPerformed(BasicFileChooserUI.java:927)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)