我是刚开始自学JAVA,在学习GUI编程时,设定菜单项open新文件,代码如下:
MenuItem mi2=new MenuItem("Open");
mi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(f,"new open file",FileDialog.LOAD);
fd.show();
//String strFile=fd.getFile();
if(fd.getFile()!=null)
{
try
{
FileInputStream fis=new FileInputStream(fd.getFile());
byte[] buf=new byte[10*1024];
int len=fis.read(buf);
ta.append(new String(buf,0,len));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}但是用eclipse运行后出现异常:
java.io.FileNotFoundException: drawings.class (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at MyPackage.HisFrame$2.actionPerformed(HisFrame.java:46)
at java.awt.MenuItem.processActionEvent(Unknown Source)
at java.awt.MenuItem.processEvent(Unknown Source)
at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)
at java.awt.MenuComponent.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)搞了好久了,一直也没有想明白,希望有哪位能够帮忙解答一下,非常感谢!
MenuItem mi2=new MenuItem("Open");
mi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(f,"new open file",FileDialog.LOAD);
fd.show();
//String strFile=fd.getFile();
if(fd.getFile()!=null)
{
try
{
FileInputStream fis=new FileInputStream(fd.getFile());
byte[] buf=new byte[10*1024];
int len=fis.read(buf);
ta.append(new String(buf,0,len));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}但是用eclipse运行后出现异常:
java.io.FileNotFoundException: drawings.class (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at MyPackage.HisFrame$2.actionPerformed(HisFrame.java:46)
at java.awt.MenuItem.processActionEvent(Unknown Source)
at java.awt.MenuItem.processEvent(Unknown Source)
at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)
at java.awt.MenuComponent.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)搞了好久了,一直也没有想明白,希望有哪位能够帮忙解答一下,非常感谢!
fd.show();
JFileChooser jfc = new JFileChooser();
if(jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
FileReader fr = new FileReader(jfc.getSelectedFile());
BufferedReader br = new BufferedReader(fr);
String temp, s = null;
while((temp = br.readLine()) != null){
s += temp;
s += "\r\n";
}
System.out.println(s);
这行是哪行
System.out.println(fd.getFile());看看输出怎样的
FileInputStream fis=new FileInputStream(fd.getFile());
这只是得到了文件名,没有路径,
FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());
修改后就正确了,在这里要谢谢各位的帮助了。