Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Simple FileNotFoundException

Hi,

Doing the most simplist input from a text file.

try
{
FileReader fin = new FileReader("menu.txt");
BufferedReader din = new BufferedReader(fin);

String line = "";

while ((line = din.readLine()) != null)
{
StringTokenizer st = new java.util.StringTokenizer(line," ");

for (int i = 0; i<dayMenu.length; i++)
{
dayMenu[i] = st.nextToken().trim();
}
}
din.close();
}

but im getting FileNotFoundException. The .txt file is in the projects directory (if i write a little output to file it goes to the same place). I just dont know why this would fail. The txt file is not read-only.

Thankyou for any help
[942 byte] By [admonrain] at [2007-11-11 8:30:00]
# 1 Re: Simple FileNotFoundException
FileNotFoundException is file not found, so you can try using the path FileReader(".\menu.txt");. or try moving the text file to another of your folders in the project. usually it should be the main folder. if you use eclipse, it should be the folder named src (eg the same folder as the root of sources, not of the project)
graviton at 2007-11-11 22:35:11 >
# 2 Re: Simple FileNotFoundException
also, you could find out where the .class files are going to after you compile, and throw the text file in there
ftr at 2007-11-11 22:36:16 >
# 3 Re: Simple FileNotFoundException
FileNotFoundException is file not found, so you can try using the path FileReader(".\menu.txt");. or try moving the text file to another of your folders in the project. usually it should be the main folder. if you use eclipse, it should be the folder named src (eg the same folder as the root of sources, not of the project)

I get an illegal space character on that so I tried \\menu.txt but to no avail. Even placing the .txt files in the folder with the .class files, .java and anywhere else doesnt seem to work :(
admonrain at 2007-11-11 22:37:15 >
# 4 Re: Simple FileNotFoundException
sorry, just try "./menu.txt". i forgot, that \ is the escape character.
graviton at 2007-11-11 22:38:14 >
# 5 Re: Simple FileNotFoundException
still no luck
admonrain at 2007-11-11 22:39:24 >
# 6 Re: Simple FileNotFoundException
Put the full path to the file in the filename.

What library files/classes have you imported?
nspils at 2007-11-11 22:40:17 >
# 7 Re: Simple FileNotFoundException
import java.net.*;
import java.util.*;
import java.io.*;
admonrain at 2007-11-11 22:41:21 >
# 8 Re: Simple FileNotFoundException
anyone know if theres any other type of filestreams that work the same way? Slightly annoying how this isnt working when theres no actual reason for it.
admonrain at 2007-11-11 22:42:19 >
# 9 Re: Simple FileNotFoundException
Have you put the entire path to the file in your "filename"? If so, what was the result?

Have you tried FileInputStream? (The benefit of FileReader is that it reads the content as characters rather than bytes which then need to be converted)
nspils at 2007-11-11 22:43:27 >
# 10 Re: Simple FileNotFoundException
Putting the full path still generated a FileNotFoundException.
admonrain at 2007-11-11 22:44:28 >
# 11 Re: Simple FileNotFoundException
hmm. did you use "/" instead of "\" symbols for the full path? can you test the existence of that file by doing System.out.println(new File("myfile").exists()); ?
graviton at 2007-11-11 22:45:20 >
# 12 Re: Simple FileNotFoundException
are you positive your file is named "menu.txt" maybe you named it "menu.txt.txt" and you have known file extensions hidden?
Joe Beam at 2007-11-11 22:46:29 >