One File Found, the other Not Found ?!?!?!

M

mcampo84

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?
 
R

Roland de Ruiter

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?
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top