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

Discussion in 'Java' started by mcampo84@gmail.com, Aug 7, 2006.

  1. Guest

    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?
    , Aug 7, 2006
    #1
    1. Advertising

  2. Guest

    Oh, btw - I forgot to mention that "cl" is a ClassLoader.

    -mc
    , Aug 7, 2006
    #2
    1. Advertising

  3. On 7-8-2006 23:14, wrote:
    > 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);
    --
    Regards,

    Roland
    Roland de Ruiter, Aug 7, 2006
    #3
  4. Guest

    Thanks, Roland. It worked like a charm!

    -mc
    , Aug 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. liking C lang.

    May output the char one after one every other second?

    liking C lang., Feb 16, 2007, in forum: C Programming
    Replies:
    16
    Views:
    542
    Keith Thompson
    Feb 17, 2007
  2. Replies:
    4
    Views:
    479
    Maett
    Jan 9, 2006
  3. TtfnJohn
    Replies:
    1
    Views:
    304
    Peter Otten
    Jun 11, 2007
  4. saify
    Replies:
    0
    Views:
    863
    saify
    Sep 28, 2009
  5. phil
    Replies:
    0
    Views:
    124
Loading...

Share This Page