运行代码时出现FileNotFoundException。 我的电影名是filecontent.java ...运行java程序时发生FileNotFoundException错误
定义:我想创建一个包含4个TextFields和4个TextAreas的程序。如果在TextField中键入文件的名称,则其内容应显示在相应的TextArea中。
错误:
例外五:java.io.FileNotFoundException:
我的代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class filecontent extends Frame implements ActionListener
{
TextField t[]=new TextField[4];
TextArea ta[]=new TextArea[4];
Button submit,exit=new Button("Exit");
Panel p1;
filecontent()
{
setGUI();
setRegister();
try{
showfile();
}
catch(IOException ioe)
{
System.out.println("Exception e : "+ioe);
}
setTitle("FileData");
setVisible(true);
setSize(300,300);
setLocation(500,200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{ System.exit(0); }
});
}
void setGUI()
{
p1=new Panel();
p1.setLayout(new GridLayout(5,4,10,10));
for(int i=0;i<4;i++)
{
t[i]=new TextField(10);
ta[i]=new TextArea();
p1.add(t[i]);
p1.add(ta[i]);
}
submit=new Button("Submit");
p1.add(submit);
p1.add(exit);
}
void setRegister()
{
submit.addActionListener(this);
exit.addActionListener(this);
}
void showfile() throws java.io.IOException
{
FileReader fin[]=new FileReader[4];
FileReader fn=new FileReader("filecontent.java");
BufferedReader br[]=new BufferedReader[4];
for(int i=0;i<4;i++)
{
fin[i]=new FileReader(t[i].getText());
}
int cnt=1;
String s;
fn=fin[0];
br[0]=new BufferedReader(fn);
while(cnt<=4)
{
if((s=br[cnt-1].readLine())!=null)
{
ta[cnt-1].append(s+"");
}
else
{
fin[cnt-1].close();
cnt++;
fn=fin[cnt-1];
br[cnt-1]=new BufferedReader(fn);
ta[cnt-1].setText("");
}
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit)
{
try{
showfile();
}
catch(IOException ioe)
{
System.out.println("Exception e"+ioe);
}
}
else if(ae.getSource()==exit)
{
System.exit(0);
}
}
public static void main(String ar[])
{
new filecontent();
}
}
而不是'System.out.println(“Exception e:”+ ioe);',把'ioe.printStackTrace()'显示出来。此外,看起来你在'NullPointerException'和'FileNotFoundException'之间感到困惑 – adarshr 2012-01-03 10:59:31
那么你会得到'NullPointerException'或'FileNotFoundException'吗? – 2012-01-03 10:59:41
@adarshr:printStackTrace()显示错误处于打开状态:** fin [i] = new FileReader(t [i] .getText()); **。我得到FileNotFoundException ...我不明白上面的行有什么问题。它从TextFiled获取文本,应该ok.so究竟是什么错误? – 2012-01-03 14:57:33