2012-01-03 81 views
0

运行代码时出现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(); 
    } 
} 
+2

而不是'System.out.println(“Exception e:”+ ioe);',把'ioe.printStackTrace()'显示出来。此外,看起来你在'NullPointerException'和'FileNotFoundException'之间感到困惑 – adarshr 2012-01-03 10:59:31

+0

那么你会得到'NullPointerException'或'FileNotFoundException'吗? – 2012-01-03 10:59:41

+0

@adarshr:printStackTrace()显示错误处于打开状态:** fin [i] = new FileReader(t [i] .getText()); **。我得到FileNotFoundException ...我不明白上面的行有什么问题。它从TextFiled获取文本,应该ok.so究竟是什么错误? – 2012-01-03 14:57:33

回答

0

你的例外可能来自这条线

FileReader fn=new FileReader("filecontent.java"); 

我认为你应该使用完整路径,而不仅仅是文件名。

0

首先,为什么不使用FileDialog代替文件的textField。其次,为了让你的程序工作,你使用的是相对路径,文件filecontent.java必须和你的.class文件位于同一个地方

0

在java中读取文件时,文件路径的语法会改变系统间的关系。所以你应该根据你使用的操作系统来应用路径。 也为您的代码文件filecontent.java应该在同一个目录中。

+0

filecontent.java是该代码所在的文件,因此我不认为与路径相关的问题出现了。 – 2012-01-03 14:00:27

1

您没有NullPointerException。你有一个FileNotFoundException。正如这个例外的名称所说,这是因为找不到您尝试打开的文件。

失败

的第一个文件的访问是这样的一个:

FileReader fn=new FileReader("filecontent.java"); 

如果您的Java文件位于一个src(或任何其他)项目的文件夹中必须添加的文件夹。例如。 src/filecontent.java

其他一些注意事项:

  • 按照约定的Java类名称以大写字母
  • 你的变量名t, ta, p1, etc.可能会造成混淆。为什么不使用textFields, textAreas, panel
  • 我想你会遇到这行while(cnt<=4) 中的ArrayIndexOutOfBoundsException。数组索引从0开始,以n - 1结尾(在你的情况下= 3)
  • 它可以帮助调试打印出catch块中的堆栈跟踪:ioe.printStackTrace()。这给你确切的行号你的代码没有
+0

感谢您的advise.I尝试ioe.printStackTrace();并在下面的行显示错误: \t ** fin [i] = new FileReader(t [i] .getText()); ** – 2012-01-03 14:07:18

+0

看起来像t [i] .getText()不包含有效的文件名。 – micha 2012-01-03 15:01:40

0

根据您的意见,得到的答复是,该文件作为a.txt出现在资源管理器中,但实际上是a.txt.txt显示文件扩展名在资源管理器避免了这个问题/混淆。


当您使用的文件路径是相对于工作目录,即应用程序在哪里运行。不在哪里可以找到源代码。如果你不知道你的工作目录是什么,你应该使用完整的路径名。

+0

我试过完整的路径名,但仍然错误... – 2012-01-03 15:49:31

+0

该文件不在您认为的位置或完整的路径名称不正确。例如,你可以做'DIR全路径名'吗? – 2012-01-03 15:51:33

+0

是的,我尝试DIR“完整路径”..它的工作原理...所以路径是正确的... – 2012-01-03 16:00:20

相关问题