file not found exception
hi,
i am using netbeans 4.1 and i want to read a txt file, i have four folders when i create a java class, those are src,build,nbproject,test.i have my file to be read and the java file both in same src director,also i have pasted th file to be read, in the build>classes also.
but still am getting the following exception,
java.io.FileNotFoundException: distance (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41).
i haevnt altered any runtime workspace.
can you help me where do i want to paste the file, so that i can read it using m program.
here is my code,
try{
FileReader fread =new FileReader("distance");
BufferedReader bread =new BufferedReader(fread);
int i=0;
do{
text = bread.readLine();
val = text.split(" ");
//value[i]= val;
//i++;
}while(text !=null);
bread.close();
new FileReader("distance"); will look for a file called distance in the directory where you program runs, or really in its current working directory, but that's usually the same.
A quick way to find the file in your src directory would be to supply the full path name of the file, like for instance:
new FileReader("c:\\myFiles\\myNetBeansProject\\src\\distance");
OleVVa at 2007-7-12 20:27:17 >

* sigh *
instead of
FileReader fread = new FileReader("distance");
BufferedReader bread = new BufferedReader(fread);
try using
InputStream is = getClass().getResourceAsStream("distance");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bread = new BufferedReader(isr);
> Or try putting your 'distance' file in the 'dist'
> directory, or the build/classes directory - one of
> those.
He says he put it in build/classes. He didn't say whether he searched under there for the class file that corresponds to the java file he talked about and once again put it in that directory.
If he placed it directly under "classes" than his version (AFAIK) should work, as long as he has the name right and doesn't need an extension.
Edit: Or does placing it in "dist" allow his version to work? I don't use IDEs, in such a way that I have to concern myself with their CWD, much.
If he placed it in the same place as the class file (which I am assumed he did since he explicitly mentioned it that way in regards to the java file) and he is using packages (which he should be but probably isn't) then he should use the getResource version. Once again checking the extension.
Edit: Directly under classes will work with the getResource version as well, as long as he adds a "/" to the front of the file name.
Right?
;-)