简体   繁体   中英

FileNotFoundException error while running java program

I am getting a FileNotFoundException while running code. my filname is filecontent.java...

Definition: I want to create a program having 4 TextFields and 4 TextAreas. If one types the name of the file in TextField, then its content should be shown in corresponding TextArea.

Error :

Exception e : java.io.FileNotFoundException :

My Code :

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();
    }
}

You don't have a NullPointerException. You have a FileNotFoundException. As the name of this exceptions says this is because a file you try to open isn't found.

The first file access that fails is this one:

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

If your java file is located within a src (or any other) folder of your project you have to add the folder. Eg src/filecontent.java

Some other notes:

  • By convention java class names start with upper case letters
  • Your variable names t, ta, p1, etc. can be confusing. Why not use textFields, textAreas, panel ?
  • I think you will run into an ArrayIndexOutOfBoundsException in this line while(cnt<=4) . Array indices start with 0 and end with n - 1 (=3 in your case)
  • It can help debuging to print out the stacktrace in your catch block: ioe.printStackTrace() . This gives you the exact line number where your code fails

Your exception may have come from this line

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

I think you should use a full path, not just a file name.

First of all, why don't you use FileDialog instead of textField for the file. Secondly, you are using relative path so for your program to work, the file filecontent.java must be in the same place as your .class file

When reading a file in java the syntax for filepath varies system to system. So you should apply the path according to the operating system you are using. Also for your code the file filecontent.java should be in the same directory.

Based on your comments, the answer is that the file appears as a.txt in explorer but is actually a.txt.txt Showing file extensions in explorer avoids this issue/confusion.


When you use a file path it is relative to the working directory, ie where the application was run. Not where the source code can be found. If you don't know what your working directory is, you should use a full path name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM