Java Resource Path Get getResourceFileRelativeToBase(final File baseDir, final String resourcePath)

Here you can find the source of getResourceFileRelativeToBase(final File baseDir, final String resourcePath)

Description

get Resource File Relative To Base

License

Apache License

Declaration

public static File getResourceFileRelativeToBase(final File baseDir, 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 {
    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 {/*from   www . j a  v  a2  s  .  c o  m*/
            uriString = baseDirUriString + "/" + resourcePath;
        }

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

Related

  1. getResourceAsString(String path)
  2. getResourceFile(Class clazz, String relPath)
  3. getResourceFile(final Class baseClass, final String path)
  4. getResourceFile(String sResourcePath, Class cRefClass)
  5. getResourceFilePath(String name)
  6. getResourceListing(Class clazz, String path)
  7. getResourceListing(Class clazz, String path)
  8. getResourceListing(Class clazz, String path)
  9. getResourceListing(Class clazz, String path, String glob)