how to list resources in a package?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: how to list resources in a package?

  1. #1
    Join Date
    Jun 2006
    Posts
    2

    Question how to list resources in a package?

    I have a set of files (a.txt, b.txt, c.txt, ....) in a java package along with code. I can access one of these using the getResourceAsStream method. However, I want to get a list of these resources, using a regular expression to find all resources that satisfy *.txt

    This needs to work whether the .class and *.txt files are on disk or in a jar file.

    Seems like something the reflection api or the Package api should do, but I can't find a way.

    Suggestions??

    Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You could scan the jar file(s) using the methods available in java.util.jar/java.util.zip, and, for the case of plain files, directory scanning using the methods in the File class. Both cases would use regex to identify the desired files.
    eschew obfuscation

  3. #3
    Join Date
    Jun 2006
    Posts
    2

    sample code

    Thanks. Here is what I came up with. I was hoping that there was a class or method that already did this.

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Enumeration;
    import java.util.regex.Pattern;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipException;
    import java.util.zip.ZipFile;

    /**
    * list resources available from the classpath
    * @author stoughto
    *
    */
    public class ResourceList {
    /**
    * for all elements of java.class.path get a Collection of resources
    * Pattern pattern = Pattern.compile(".*"); gets all resources
    * @param pattern the pattern to match
    * @return the resources in the order they are found
    */
    public static Collection<String> getResources(Pattern pattern) {
    ArrayList<String> retval = new ArrayList<String>();
    String classPath = System.getProperty("java.class.path",".");
    String[] classPathElements = classPath.split(":");
    for (String element : classPathElements) {
    retval.addAll(getResources(element, pattern));
    }
    return retval;
    }

    private static Collection<String> getResources(String element, Pattern pattern) {
    ArrayList<String> retval = new ArrayList<String>();
    File file = new File(element);
    if (file.isDirectory()) {
    retval.addAll(getResourcesFromDirectory(file, pattern));
    } else {
    retval.addAll(getResourcesFromJarFile(file, pattern));
    }
    return retval;
    }

    private static Collection<String> getResourcesFromJarFile(File file, Pattern pattern) {
    ArrayList<String> retval = new ArrayList<String>();
    ZipFile zf;
    try {
    zf = new ZipFile(file);
    } catch (ZipException e) {
    throw new Error(e);
    } catch (IOException e) {
    throw new Error(e);
    }
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) e.nextElement();
    String fileName = ze.getName();
    boolean accept = pattern.matcher(fileName).matches();
    if (accept) {
    retval.add(fileName);
    }
    }
    try {
    zf.close();
    } catch (IOException e1) {
    throw new Error(e1);
    }
    return retval;
    }

    private static Collection<String> getResourcesFromDirectory(File directory, Pattern pattern) {
    ArrayList<String> retval = new ArrayList<String>();
    File[] fileList = directory.listFiles();
    for (File file : fileList) {
    if (file.isDirectory()) {
    retval.addAll(getResourcesFromDirectory(file, pattern));
    } else {
    try {
    String fileName = file.getCanonicalPath();
    boolean accept = pattern.matcher(fileName).matches();
    if (accept) {
    retval.add(fileName);
    }
    } catch (IOException e) {
    throw new Error(e);
    }
    }
    }
    return retval;
    }

    /**
    * list the resources that match args[0]
    * @param args args[0] is the pattern to match, or list all resources if there are no args
    */
    public static void main(String[] args) {
    Pattern pattern;
    if (args.length < 1) {
    pattern = Pattern.compile(".*");
    } else {
    pattern = Pattern.compile(args[0]);
    }
    Collection<String> list = ResourceList.getResources(pattern);
    for (String name : list) {
    System.out.println(name);
    }

    }
    }

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2002, 07:51 PM
  2. list of Open source resources + projects
    By Anonymous in forum Open Source
    Replies: 0
    Last Post: 04-24-2002, 01:20 AM
  3. update: list of XML resources (2002-01-29)
    By Anonymous in forum XML
    Replies: 0
    Last Post: 01-30-2002, 01:33 AM
  4. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 01:22 PM
  5. dropdown boxes
    By Matt in forum Web
    Replies: 5
    Last Post: 03-22-2001, 07:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center