I am using NetBeans (5.0) to write an applet that requires reading from
an input file - for now let's call it "testInput.txt". In order to
read from the file I use a BufferedReader with the following code:
FileReader inputFile = new
FileReader(String.valueOf(cl.getResource(
"data/testInput.txt")));
BufferedReader in = new BufferedReader(inputFile);
I created a "data" subfolder in the "src" folder that NetBeans provides
and that's where testInput.txt resides (NetBeans bundles everything in
this folder into the jar when it compiles). When I run the program,
however, I get the following error:
java.io.FileNotFoundException:
<pathName>.<jarName>.jar!\testInput.txt
(The filename, directory name, or volume label syntax is
incorrect).
What is confusing me is that earlier in the same program, I read from
another file in the same directory - "testInput.xml" - and use the same
code as above, with "testInput.xml" replacing "testInput.txt".
Can anyone help me figure this one out?
mcampo84@gmail.com - 07 Aug 2006 22:22 GMT
Oh, btw - I forgot to mention that "cl" is a ClassLoader.
-mc
Roland de Ruiter - 07 Aug 2006 22:32 GMT
> I am using NetBeans (5.0) to write an applet that requires reading from
> an input file - for now let's call it "testInput.txt". In order to
[quoted text clipped - 20 lines]
>
> Can anyone help me figure this one out?
FileReader is capable of reading a resource (i.e. a file) from the
filesystem, not from a resource in a jar file.
Try the following instead:
// Assuming cl instanceof ClassLoader
final String resourceName = "data/testInput.txt";
final String resourceEncoding = "ISO-8859-1";
// or: = "UTF-8";
// or: = System.getProperty("file.encoding");
// It should match encoding of data/testInput.txt
InputStreamReader inputReader = new
InputStreamReader(cl.getResourceAsStream(resourceName),
resourceEncoding);
BufferedReader in = new BufferedReader(inputReader);

Signature
Regards,
Roland
mcampo84@gmail.com - 07 Aug 2006 22:49 GMT
Thanks, Roland. It worked like a charm!
-mc