Java Resource Load getResourceAsStream(String name)

Here you can find the source of getResourceAsStream(String name)

Description

Load resource as stream if it is present somewhere in the classpath.

License

LGPL

Parameter

Parameter Description
name Name of the resource

Exception

Parameter Description
IOException if resource can't be found

Return

instance of an input stream or null if the resource couldn't be found

Declaration

public static InputStream getResourceAsStream(String name) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*ww  w. j a  va 2s  .  co m*/
     * Load resource as stream if it is present somewhere in the classpath.
     * @param name Name of the resource
     * @return instance of an input stream or <code>null</code> if the resource couldn't be found
     * @throws IOException if resource can't be found
     */
    public static InputStream getResourceAsStream(String name) throws IOException {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        if (in == null) {
            throw new IOException("could not find " + name + " in classpath");
        }

        return in;
    }

    /**
     * Load a resource as stream from a given <code>folder</code>.
     * 
     * @see #getResourceAsStream(String)
     */
    public static InputStream getResourceAsStream(String folder, String name) throws IOException {
        return getResourceAsStream(folder + File.separator + name);
    }
}

Related

  1. getResourceAsStream(String filename)
  2. getResourceAsStream(String filePath)
  3. getResourceAsStream(String name)
  4. getResourceAsStream(String name)
  5. getResourceAsStream(String name)
  6. getResourceAsStream(String name, Class aClass)
  7. getResourceAsStream(String name, Class clazz)
  8. getResourceAsStream(String name, Class clazz)
  9. getResourceAsStream(String name, Class clzz)