Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
mcampo84@gmail.com - 07 Aug 2006 22:14 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.