Creates a reader for a resource in the relative path - Java java.util

Java examples for java.util:ResourceBundle

Description

Creates a reader for a resource in the relative path

Demo Code


import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main{
    public static void main(String[] argv) throws Exception{
        String relativePath = "java2s.com";
        System.out.println(getResourceAsReader(relativePath));
    }/*from  w  w w . j  a  va  2  s.  c  o m*/
    /**
     * Creates a reader for a resource in the relative path
     * @param relativePath relative path of the resource to be read
     * @return a reader of the resource
     */
    public static Reader getResourceAsReader(String relativePath) {
        try {
            return new InputStreamReader(
                    Main.class.getResourceAsStream(relativePath),
                    "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Unable to read input", e);
        }
    }
    /**
     * This is a convenience method to load a resource as a stream.
     *
     * The algorithm used to find the resource is given in getResource()
     *
     * @param resourceName The name of the resource to load
     * @param callingClass The Class object of the calling object
     * @return  the InputStream of the specific resource on the 'resources' folder.
     */
    public static InputStream getResourceAsStream(String resourceName,
            Class<?> callingClass) {
        URL url = getResourceAsURL(resourceName, callingClass);
        try {
            return (url != null) ? url.openStream() : null;
        } catch (IOException e) {
            return null;
        }
    }
    public static InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        if (cl == null) {
            // A system class.
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
    }
    /**
     * Load a given resource.
     *
     * This method will try to load the resource using the following methods (in order):
     * <ul>
     *  <li>From Thread.currentThread().getContextClassLoader()
     *  <li>From Main.class.getClassLoader()
     *  <li>callingClass.getClassLoader()
     * </ul>
     *
     * @param resourceName The name IllegalStateException("Unable to call ")of the resource to load
     * @param callingClass The Class object of the calling object
     * @return the URL of the specific resource on the 'resources' folder.
     */
    public static URL getResourceAsURL(String resourceName,
            Class<?> callingClass) {
        URL url = Thread.currentThread().getContextClassLoader()
                .getResource(resourceName);

        if (url == null) {
            url = Main.class.getClassLoader().getResource(
                    resourceName);
        }
        if (url == null) {
            ClassLoader cl = callingClass.getClassLoader();

            if (cl != null) {
                url = cl.getResource(resourceName);
            }
        }

        if ((url == null)
                && (resourceName != null)
                && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) {
            return getResourceAsURL('/' + resourceName, callingClass);
        }

        return url;
    }
    /**
     * Add a package name prefix if the name is not absolute Remove leading "/"
     * if name is absolute
     * @param name the {@link String} of the resources.
     * @return the {@link String} new name.
     */
    private static String resolveName(String name) {
        if (name == null)
            return null;
        if (!name.startsWith("/")) {
            Class<?> c = Main.class;
            while (c.isArray()) {
                c = c.getComponentType();
            }
            String baseName = c.getName();
            int index = baseName.lastIndexOf('.');
            if (index != -1) {
                name = baseName.substring(0, index).replace('.', '/') + "/"
                        + name;
            }
        } else {
            name = name.substring(1);
        }
        return name;
    }
}

Related Tutorials