Netbeans FileReader FileNotFound Exception when the file is in folder?

♀尐吖头ヾ 提交于 2019-12-02 00:41:15
Vineet Menon

In netbeans the default working directory is always the root folder, i mean the folder which contains the folders which name "src", "build" etc. Place the file along with these folders and it will do the trick.

Here is step by Step procedure in NetBeans IDE 7.0.1

  1. Click on File menu.
  2. Click on Project Properties.
  3. In the categories, select Run.
  4. In main class you select your current java file.
  5. In Arguments select the file you want to read for e.g. abc.txt or abc.java
  6. And in Working Directory write down the path of folder in which this abc.txt or abc.java lies.
  7. Click OK to close Project Properties.
  8. While running your program don't forget to select your project as Main Project.
  9. Then click F^ on keyboard. i.e. You have to raun your main project instead of just running your current java file. That's it....enjoy!!!!

Finally found the solution

In eclipse you should put the target file in project folder. Guess same applies to NetBeans.

I had my target file in "src" folder (where the actual code files were). In fact i had to just change it to upper folder where the project folder is.

Easy and simple.

Probably you have different "working directories" in you different setups. You can check which directory you are in by printing it like this:

System.out.println(new File(".").getAbsoluteFile());

In eclipse you can set set up the working directory in the run configurations, arguments tab.

Try BufferedReader?

EDIT: Edited to show example more closely to your code. I got excepption when using File Reader. But was able to .println with BufferedReader. I did not use Scanner.

EDIT2: I was also able to get your code to work. With Scanner etc(When using full path) (Example: FileReader read = new FileReader(""C:\\myfolder\\folder\\a.txt". So hmmm.

  try {
  list = new LinkedList<Patient>();
  BufferedReader scan = new BufferedReader(new FileReader("C:\\a.txt"));
  String lines;
            try {
                // Scanner scan = new Scanner(read);
                while ((lines = scan.readLine()) != null) {
                 //I just printed lines you will do your stuff here
                    System.out.println(lines);
                    }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(null, "The file does not exist!" + "\nProgram is terminating.", "File Not Found", JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);     
        }

right click on your text file select properties and copy the path and paste it in the place where you have entered your file name

lets say you wanna add test.txt in netbeans

if your project in C:\myProject put the text file inside C:\myProject file directly not in the C:\myProject\src . then use:

File file = new File("test.txt");

Scanner in = new Scanner(file);

OR

Scanner input = new Scanner(new File("test.txt"));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!