Java Resource Load getResourceAsReader(String relativeName, Class clazz, String enc)

Here you can find the source of getResourceAsReader(String relativeName, Class clazz, String enc)

Description

Returns the reader for the resource with the specified relative name.

License

MIT License

Parameter

Parameter Description
relativeName a resource name (relative, or absolute).
clazz a class used to make the resource name absolute and load the resource.
enc the resource encoding.

Exception

Parameter Description

Return

the resource reader.

Declaration

public static Reader getResourceAsReader(String relativeName,
        Class<?> clazz, String enc) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  j a va 2  s.  c o m
 * Copyright (c) 2013-2017 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

public class Main {
    /**
     * Returns the reader for the resource with the specified absolute name.
     * The resource is loaded through the current thread's context class loader.
     *
     * @param absoluteName a resource name.
     * @param enc the resource encoding.
     * @return the resource reader.
     * @throws java.io.IOException if the resource was not found.
     */
    public static Reader getResourceAsReader(String absoluteName, String enc)
            throws IOException {
        InputStream ins = getResourceAsStream(absoluteName);
        return new InputStreamReader(ins, enc);
    }

    /**
     * Returns the reader for the resource with the specified relative name.
     * The name is treated as relative, because the package name of the specified
     * class is prepended to that name to make the name absolute. The resource
     * is loaded through the current thread's context class loader.
     *
     * @param relativeName a resource name (relative, or absolute).
     * @param clazz a class used to make the resource name absolute and load the resource.
     * @param enc the resource encoding.
     * @return the resource reader.
     * @throws java.io.IOException if the resource was not found.
     */
    public static Reader getResourceAsReader(String relativeName,
            Class<?> clazz, String enc) throws IOException {
        String newName = getAbsoluteResourceName(relativeName, clazz);
        return getResourceAsReader(newName, enc);
    }

    /**
     * Returns the input stream for the resource with the specified absolute name.
     * The resource is loaded through the current thread's context class loader.
     *
     * @param absoluteName a resource name.
     * @return the resource input stream.
     * @throws java.io.IOException if the resource was not found.
     */
    public static InputStream getResourceAsStream(String absoluteName)
            throws IOException {
        String resourceName = absoluteName.startsWith("/") ? absoluteName
                .substring(1) : absoluteName;
        InputStream ins = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(resourceName);

        if (ins == null)
            throw new IOException("Cannot find resource: " + resourceName);

        return new BufferedInputStream(ins);
    }

    /**
     * Returns the input stream for the resource with the specified relative name.
     * The name is treated as relative and the package name of the specified
     * class is prepended to that name to make the name absolute. The resource
     * is loaded through the current thread's context class loader.
     *
     * @param relativeName a resource name (relative, or absolute).
     * @param clazz a class used to make the resource name absolute and load the resource.
     * @return the resource input stream.
     * @throws java.io.IOException if the resource was not found.
     */
    public static InputStream getResourceAsStream(String relativeName,
            Class<?> clazz) throws IOException {
        String newName = getAbsoluteResourceName(relativeName, clazz);
        return getResourceAsStream(newName);
    }

    /**
     * Simply prepends the package name of the specified class to the
     * specified name and replaces all '.' with '/'.
     *
     * @param name a resource name.
     * @param clazz a class to be used to resolve the name if it
     * is a relative name.
     * @return the absolute resource name.
     */
    public static String getAbsoluteResourceName(String name, Class<?> clazz) {
        StringBuilder absName = new StringBuilder(clazz.getPackage()
                .getName().replace('.', '/'));
        absName.append('/');
        absName.append(name);
        return absName.toString();
    }
}

Related

  1. getResourceAsFile(T testSuite, String resourceName)
  2. getResourceAsInputStream(Class resourceClass, String resourceName)
  3. getResourceAsProperties(String resource)
  4. getResourceAsReader(final Class classLocation, final String resourceName)
  5. getResourceAsReader(String name)
  6. getResourceAsStream(Class baseclass, String name)
  7. getResourceAsStream(Class claz, String name)
  8. getResourceAsStream(Class clazz, String name)
  9. getResourceAsStream(Class clazz, String resource)