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 / November 2005

Tip: Looking for answers? Try searching our database.

Getting the directory of resources

Thread view: 
Paul J. Lucas - 01 Nov 2005 21:21 GMT
    Ordinarily, to read a resource, I do:

    InputStream is = MyClass.class.getResourcesAsStream( "foo" );

    But what I want to do now is to get the directory that resource
    is in because I want to scan that directory for all the
    resources in it, not just foo.

    But there doesn't seem to be an obvious way to do this.  How can
    I do what I want?

    - Paul
Andrew Thompson - 01 Nov 2005 21:36 GMT
>     InputStream is = MyClass.class.getResourcesAsStream( "foo" );
>
>     But what I want to do now is to get the directory that resource
>     is in because I want to scan that directory for all the
>     resources in it, not just foo.

  URL urlToFoo = MyClass.class.getResource("foo");
  File fooFile = new File( urlToFoo.toURI() );
  File fooDir = fooFile.getParentFile();
  File[] allFooDirFiles = fooDir.listFiles();
  ...

>     But there doesn't seem to be an obvious way to do this.  

You cannot do 'file lists' on URL's, and there is no
way to determine all resources available to a classpath.
Rhino - 01 Nov 2005 22:37 GMT
> > InputStream is = MyClass.class.getResourcesAsStream( "foo" );
> >
[quoted text clipped - 12 lines]
> You cannot do 'file lists' on URL's, and there is no
> way to determine all resources available to a classpath.

What do you mean by that?

A few lines of code will tell you the names of all of the files in your
classpath. Once you know the names of those files, which are typically jars,
you can easily open each jar and get information on what files are in the
jar. You can, of course, also open those files and use them for whatever you
want.

Here's the code I use:

------------
   /*
    * Determine the current classpath. The classpath includes, among other
things, the
    * names of all jars that are on the classpath.
    */
   String classPath = System.getProperty("java.class.path");

   /*
    * Tokenize the classpath. Examine each directory and file until the
desired jar is found.
    * If the jar is found, get a JarFile reference to it, then break out of
the while loop.
    */
   StringTokenizer stringTokenizer = new StringTokenizer(classPath,
System.getProperty("path.separator"));
   String token = null;
   File file = null;

   while (stringTokenizer.hasMoreTokens()) {
       token = stringTokenizer.nextToken();
       /* Get a file reference to the path or file in the token. */
       file = new File(token);
       /* Skip directories. */
       if (file.isDirectory())
           continue;
       /* Do whatever you want with the file. If it is a jar, you could
determine what files are in it, or whatever. */
       String fileName = file.getName();
       if (DEBUG) System.out.println("File name is: " + fileName);
               break;
           }
           catch(Exception excp) {
            //error handling             }
       } else {
           continue;
       }
   } //end while

------------

Doesn't that "determine all resources available to a classpath"?

Rhino
Paul J. Lucas - 02 Nov 2005 18:50 GMT
> >       InputStream is = MyClass.class.getResourcesAsStream( "foo" );
> >
[quoted text clipped - 6 lines]
>    File fooDir = fooFile.getParentFile();
>    File[] allFooDirFiles = fooDir.listFiles();

    I probably should have mentioned that I'd like a JDK 1.4.x
    solution since the above doesn't compile in 1.4.2.

    Substituting toString() for toURI() compiles, but allFooDirFiles
    is always null.

    - Paul
Andrew Thompson - 03 Nov 2005 01:22 GMT
>>>      InputStream is = MyClass.class.getResourcesAsStream( "foo" );
>>>
[quoted text clipped - 12 lines]
>     Substituting toString() for toURI() compiles, but allFooDirFiles
>     is always null.

Did you check..
  fooFile.exists()
?

Did you check..
  urlToFoo!=null
?
Paul J. Lucas - 03 Nov 2005 18:05 GMT
> >       I probably should have mentioned that I'd like a JDK 1.4.x
> >       solution since the above doesn't compile in 1.4.2.
[quoted text clipped - 9 lines]
>    urlToFoo!=null
> ?

    Yes.

    - Paul
Thomas Hawtin - 02 Nov 2005 08:06 GMT
>     Ordinarily, to read a resource, I do:
>
[quoted text clipped - 6 lines]
>     But there doesn't seem to be an obvious way to do this.  How can
>     I do what I want?

In the beginning (JDK 1.0) classes were loaded individually. In the case
of applets over HTTP. HTTP doesn't provide lists of directories. So, in
general, you cannot always find the answer.

For Locale.getAvailableLocales, Sun's JRE scans the directories and
zip/jar files on the classpath. Prior to Java SE 6, Sun's code is pretty
slow. For instance, it's better to use a ZipInputStream than zip around
a ZipFile.

The easiest solution is to list the resources you want.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 02 Nov 2005 11:46 GMT
On Wed, 02 Nov 2005 07:06:17 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :

>In the beginning (JDK 1.0) classes were loaded individually.

What happens now?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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.