-
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
-
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
-
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
-
By Anonymous in forum Open Source
Replies: 0
Last Post: 06-08-2002, 07:51 PM
-
By Anonymous in forum Open Source
Replies: 0
Last Post: 04-24-2002, 01:20 AM
-
By Anonymous in forum XML
Replies: 0
Last Post: 01-30-2002, 01:33 AM
-
By Larry Rebich in forum vb.announcements
Replies: 1
Last Post: 06-28-2001, 01:22 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks