Java Resource Path Get getAlternateResourceFile(final String resourcePath)

Here you can find the source of getAlternateResourceFile(final String resourcePath)

Description

get Alternate Resource File

License

Apache License

Declaration

public static File getAlternateResourceFile(final String resourcePath) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**/*  ww  w  . j  a  va 2s  .com*/
     * A configurable, alternate location to search for resources, if not found in the classpath.
     */
    public static File alternateResourceRootFolder = null;

    public static File getAlternateResourceFile(final String resourcePath) {
        return getResourceFileRelativeToBase(alternateResourceRootFolder, resourcePath);
    }

    public static File getResourceFileRelativeToBase(final File baseDir, final String resourcePath) {

        final String baseDirUriString = baseDir != null ? baseDir.toURI().toString() : "file:/";
        final boolean baseEndsWithSlash = baseDirUriString.endsWith("/");
        final boolean resourcePathBeginsWithSlash = resourcePath.startsWith("/");
        final String uriString;
        if (baseEndsWithSlash != resourcePathBeginsWithSlash) {
            uriString = baseDirUriString + resourcePath;
        } else if (baseEndsWithSlash) {
            uriString = baseDirUriString.substring(0, baseDirUriString.length() - 1) + resourcePath;
        } else {
            uriString = baseDirUriString + "/" + resourcePath;
        }

        try {
            File f = new File(new URI(uriString));
            return f;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. classToResource(String className)
  2. deserialize(Class parentOfResource, String resourcePath, Class targetClass)
  3. existsResource(String pathName)
  4. getAbsolutePathFromResource(Class reference, String resource)
  5. getAbsoluteResource(String path)
  6. getBasePath(Class clazz, String resource)
  7. getChildResources(String path)
  8. getClassResource(Class clazz, String resPath)
  9. getClassResources(Class clazz, String resPath)