Netbeans FileReader FileNotFound Exception when the file is in folder?

20,968

Solution 1

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.

Solution 2

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!!!!

Solution 3

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.

Share:
20,968
Ben Mattaini
Author by

Ben Mattaini

Updated on July 09, 2022

Comments

  • Ben Mattaini
    Ben Mattaini almost 2 years

    so the problem is that I am having exception thrown each time I try to load the code below on NetBeans or Eclips, but when I try to run it thru TextMate everything works fine!

    I tried to put the absolute address, changed the text file etc.. didn't help!

    Can someone help me or tell why it won't run with IDE?

    Thanks

    void loadFile() {
        try {
            list = new LinkedList<Patient>();
    
            FileReader read = new FileReader("a.txt");
            Scanner scan = new Scanner(read);
    
            while (scan.hasNextLine()) {
                String Line = scan.nextLine();
                String[] subArray = new String[5];
                subArray = Line.split(",");
                int a = Integer.parseInt(subArray[4]);
    
                list.add(new Patient(Integer.parseInt(subArray[0]), subArray[1], subArray[2], subArray[3], a));
            }
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "The file does not exist!" + "\nProgram is terminating.", "File Not Found", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
        cap = list.size();
        search_names = new int[cap];
        for (int i = 0; i < list.size(); i++) {
            search_names[i] = i;
        }
        setNames(search_names);
    }//end loadFile
    

    Debug log: Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar Have no file for /System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar }

  • dacwe
    dacwe over 12 years
    Updated (for eclipse) (programmatically you can do System.out.println(new File(".").getAbsoluteFile());)
  • Ben Mattaini
    Ben Mattaini over 12 years
    The a.txt file is in that "root" folder with .class files that calls it! Example: Users/ME/NetBeansProjects/Project1/src All the files are in this folder!
  • Vineet Menon
    Vineet Menon over 12 years
    try this path..Users/ME/NetBeansProjects/Project1/a.txt
  • Ben Mattaini
    Ben Mattaini over 12 years
    Not Sure... maybe it would work this way but I have to use LinkedList as it is "requirement" :) - BufferedReader doesn't like LinkedList
  • sealz
    sealz over 12 years
    @Ben Mattaini I edited to show what I was able to get working. I commented out a bunch of your code etc. But once I specified exact path in buffered reader it worked.
  • Rudolf Real
    Rudolf Real over 10 years
    In Eclipse, if you want to put the file in other location use: new FileReader("src/package/file.txt");
  • Rudolf Real
    Rudolf Real over 10 years
    Other alternative, you can define the relative path form the project: new FileReader("src/package/file.txt");