Description
List directory contents for a resource folder.
License
Open Source License
Parameter
Parameter | Description |
---|
clazz | Any java class that lives in the same place as the resources you want. |
path | Should end with "/", but not start with one. |
glob | The pattern to match |
Exception
Parameter | Description |
---|
URISyntaxException | an exception |
IOException | an exception |
Return
Just the name of each member item, not the full paths.
Declaration
public static List<String> getResourceListing(Class<?> clazz, String path, String glob)
Method Source Code
//package com.java2s;
/*/*from w w w . ja va 2 s . c o m*/
LabPal, a versatile environment for running experiments on a computer
Copyright (C) 2015-2017 Sylvain Hall?
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
/**
* List directory contents for a resource folder. Not recursive.
* This is basically a brute-force implementation.
* Works for regular files and also JARs.
* <p>
* This method was authored by Greg Briggs.
* @param clazz Any java class that lives in the same place as the resources you want.
* @param path Should end with "/", but not start with one.
* @param glob The pattern to match
* @return Just the name of each member item, not the full paths.
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> getResourceListing(Class<?> clazz, String path, String glob) {
return getResourceListing(clazz, path, glob, "");
}
/**
* List directory contents for a resource folder. Not recursive.
* This is basically a brute-force implementation.
* Works for regular files and also JARs.
* <p>
* This method was authored by Greg Briggs.
* @param clazz Any java class that lives in the same place as the resources you want.
* @param path Should end with "/", but not start with one.
* @param glob The pattern to match
* @param exclude_glob The pattern to exclude files
* @return Just the name of each member item, not the full paths.
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> getResourceListing(Class<?> clazz, String path, String glob, String exclude_glob) {
List<String> names = new ArrayList<String>();
try {
URL dirURL = clazz.getClassLoader().getResource(path);
if (dirURL != null && dirURL.getProtocol().equals("file")) {
/* A file path: easy enough */
URI uri = dirURL.toURI();
File f = new File(uri);
for (String filename : f.list()) {
String f_filename = uri.getRawPath() + filename;
File f2 = new File(f_filename);
if (f2 != null && f2.isDirectory()) {
filename += "/";
}
if (filename.matches(glob)) {
if (!filename.matches(exclude_glob)) {
names.add(filename);
}
}
}
return names;
}
if (dirURL == null) {
/*
* In case of a jar file, we can't actually find a directory.
* Have to assume the same jar as clazz.
*/
String me = clazz.getName().replace(".", "/") + ".class";
dirURL = clazz.getClassLoader().getResource(me);
}
if (dirURL.getProtocol().equals("jar")) {
/* A JAR path */
String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith(path)) { //filter according to the path
String entry = name.substring(path.length());
/*int checkSubdir = entry.indexOf("/");
if (checkSubdir >= 0)
{
// if it is a subdirectory, we just return the directory name
entry = entry.substring(0, checkSubdir);
}*/
if (entry.matches(glob)) {
result.add(entry);
}
}
}
jar.close();
names.addAll(result);
return names;
}
} catch (IOException e) {
return names;
} catch (RuntimeException e) {
return names;
} catch (URISyntaxException e) {
return names;
}
return names;
}
}
Related
- getResourceFilePath(String name)
- getResourceFileRelativeToBase(final File baseDir, final String resourcePath)
- getResourceListing(Class clazz, String path)
- getResourceListing(Class> clazz, String path)
- getResourceListing(Class> clazz, String path)
- getResourcePath()
- getResourcePath(Class> clazz, String fileName)
- getResourcePath(final Class> bundleClazz, final String pathToFile)
- getResourcePath(Object object, String resource)