Java Resource Load getResourceAsReader(final Class classLocation, final String resourceName)

Here you can find the source of getResourceAsReader(final Class classLocation, final String resourceName)

Description

Get a ressource in classpath and convert it as Reader.

License

Open Source License

Parameter

Parameter Description
classLocation This class allows to find the package which is located the ressoure
resourceName The name of ressource

Exception

Parameter Description
IOException In case of IO error

Return

The Reader for the ressource

Declaration

public static Reader getResourceAsReader(final Class<?> classLocation, final String resourceName)
        throws IOException 

Method Source Code

//package com.java2s;
/**//ww w.j  ava2  s  . c o m
 *
 * SIROCCO
 * Copyright (C) 2011 France Telecom
 * Contact: sirocco@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * $Id$
 *
 */

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

public class Main {
    /**
     * Get a ressource in classpath and convert it as Reader.
     * 
     * @param classLocation This class allows to find the package which is
     *        located the ressoure
     * @param resourceName The name of ressource
     * @return The Reader for the ressource
     * @throws IOException In case of IO error
     */
    public static Reader getResourceAsReader(final Class<?> classLocation, final String resourceName)
            throws IOException {
        InputStream in = classLocation.getResourceAsStream(resourceName);
        if (null == in) {
            throw new IOException("Resource not found : " + resourceName);
        }
        return new InputStreamReader(in);
    }
}

Related

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