Netbeans FileReader FileNotFound Exception when the file is in folder?
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
- Click on File menu.
- Click on Project Properties.
- In the categories, select Run.
- In main class you select your current java file.
- In Arguments select the file you want to read for e.g. abc.txt or abc.java
- And in Working Directory write down the path of folder in which this abc.txt or abc.java lies.
- Click OK to close Project Properties.
- While running your program don't forget to select your project as Main Project.
- 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.
Ben Mattaini
Updated on July 09, 2022Comments
-
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 over 12 yearsUpdated (for eclipse) (programmatically you can do
System.out.println(new File(".").getAbsoluteFile());
) -
Ben Mattaini over 12 yearsThe 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 over 12 yearstry this path..Users/ME/NetBeansProjects/Project1/a.txt
-
Ben Mattaini over 12 yearsNot Sure... maybe it would work this way but I have to use LinkedList as it is "requirement" :) - BufferedReader doesn't like LinkedList
-
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 over 10 yearsIn Eclipse, if you want to put the file in other location use:
new FileReader("src/package/file.txt");
-
Rudolf Real over 10 yearsOther alternative, you can define the relative path form the project:
new FileReader("src/package/file.txt");