Java i/o using netbeans

Danimal1209

Senior member
Nov 9, 2011
355
0
0
In my class we are reading lines from a file. My teacher has told us to just make sure the file we are reading from is in the same folder as the .java files, which it is. But, using netbeans, the whole try/catch is being skipped when the compiler hits the FileReader line. I am assuming this is because it cannot locate the file.
Does Netbeans need the file placed in a different folder?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Assuming you are referencing the file without a fully-qualified path, you'll want the file in the same location as your compiled .class file. NetBeans probably places those in a different directory from the .java source files, so you'll need to examine your folder structure a bit to see where the file should go.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
It's in the same folder that the class files are as well.

what is the syntax to show the path of the file? We haven't gotten there yet.
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
File f = new File("abc.txt");
String fullPath = f.getAbsolutePath();

Look at fullPath (print it out etc) and you'll know where it thinks the file is meant to be.
 

beginner99

Diamond Member
Jun 2, 2009
5,318
1,763
136
The file needs to be in the project folder and not where the class files are. So if your project folder is c:\myproject put it in that folder.
 

N4g4rok

Senior member
Sep 21, 2011
285
0
0
You may need to open the Properties dialog for that specific project and make sure that those folders containing your external files are added to the sources section. Also, unless you specify a package, Netbeans will look for a default package, which some projects aren't created with depending on how they were set up.