Java Utililty Methods Resource Load

List of utility methods to do Resource Load

Description

The list of methods to do Resource Load are organized into topic(s).

Method

FilegetResourceFile(Class theClass, String fileName)
get Resource File
return new File(theClass.getResource(fileName).getFile());
FilegetResourceFile(String relativeResourceFilePathname)
get Resource File
return new File(getResource(relativeResourceFilePathname).toURI());
FilegetResourceFile(String resource)
Try to discover the File for the test resource
File file = new File(resource);
if (file.exists())
    return file;
file = new File(getTestResourcesDir() + "/" + resource);
if (file.exists())
    return file;
String notSet = (getTestResourcesDir() == null
        ? " System property '" + SYSPROP_TEST_RESOURCES_DIRECTORY + "' not set."
...
FilegetResourceFile(String resourceName)
get Resource File
return new File(ClassLoader.getSystemClassLoader().getResource(resourceName).toURI());
InputStreamgetResourceFromJar(File fromFile, String insideFile)
get Resource From Jar
if (fromFile.exists() && fromFile.isFile()) {
    if (fromFile.getName().endsWith(".jar")) {
        try {
            JarFile jarFile = new JarFile(fromFile);
            InputStream returnMe = jarFile.getInputStream(jarFile.getJarEntry(insideFile));
            jarFile.close();
            return returnMe;
        } catch (IOException e) {
...
voidgetResourceInDirPackage(String packageName, String packagePath, final boolean recursive, List classes)
get Resource In Dir Package
File dir = new File(packagePath);
if (!dir.exists() || !dir.isDirectory()) {
    return;
File[] dirfiles = dir.listFiles();
for (File file : dirfiles) {
    if (file.isDirectory()) {
        getResourceInDirPackage(packageName + "." + file.getName(), file.getAbsolutePath(), recursive,
...
String[]getResourceList(File fdir, String extn)
get Resource List
ArrayList<String> als = new ArrayList<String>();
for (File f : fdir.listFiles()) {
    String fnm = f.getName();
    if (fnm.endsWith(extn)) {
        als.add(fnm.substring(0, fnm.length())); 
return als.toArray(new String[als.size()]);
...
SetgetResourceListing(URL pathUrl, String prefix, String suffix)
get Resource Listing
Set<String> result = new HashSet<>();
if ("file".equals(pathUrl.getProtocol())) {
    for (String name : new File(pathUrl.toURI()).list()) {
        if (name.endsWith(suffix)) {
            result.add(prefix + "/" + name);
if ("jar".equals(pathUrl.getProtocol())) {
    String jarPath = pathUrl.getPath().substring(5, pathUrl.getPath().indexOf("!")); 
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
    for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        String name = entries.nextElement().getName();
        if (name.startsWith(prefix) && name.endsWith(suffix)) {
            result.add(name);
    jar.close();
return result;
StringgetResourceNameFromFileName(final String pFileName)
a/b/c.java -> a/b/c.java a\b\c.java -> a/b/c.java
if ('/' == File.separatorChar) {
    return pFileName;
return pFileName.replace(File.separatorChar, '/');
SetgetResourceNames(File dir)
Returns the resource names contained in a directory, and for directory resource, a trailing '/' is added
Set<String> resourceNames = new HashSet<String>();
String[] resArray = dir.list();
if (resArray != null) {
    for (int i = 0; i < resArray.length; i++) {
        if (new File(dir, resArray[i]).isDirectory())
            resArray[i] += '/';
    resourceNames.addAll(Arrays.asList(resArray));
...