Java Resource Load getResourceAsStream(String filename)

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

Description

Get a resource as a stream.

License

LGPL

Parameter

Parameter Description
filename a parameter

Declaration

public static InputStream getResourceAsStream(String filename) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Shapeways, Inc Copyright (c) 2011
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import java.io.File;
import java.io.FileInputStream;

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

public class Main {
    /**/*w  w w.  ja  v  a2s.  c om*/
     * Get a resource as a stream.  Deals with differences between loading from an app and applet.
     * @param filename
     * @return
     */
    public static InputStream getResourceAsStream(String filename) {
        File f = new File(filename);

        try {
            if (f.exists()) {
                return new FileInputStream(f);
            } else {

                String fname = "classes" + File.separator + filename;
                f = new File(fname);

                if (f.exists()) {
                    return new FileInputStream(f);
                } else {
                    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                    fname = filename.replace("\\", "/");
                    InputStream is = classLoader.getResourceAsStream(fname);
                    return is;
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return null;
        }
    }
}

Related

  1. getResourceAsStream(final String resourceName, final Class caller)
  2. getResourceAsStream(final String resourceName, final Class caller)
  3. getResourceAsStream(Object context, String resourceName)
  4. getResourceAsStream(Object o, String resource)
  5. getResourceAsStream(String filename)
  6. getResourceAsStream(String filePath)
  7. getResourceAsStream(String name)
  8. getResourceAsStream(String name)
  9. getResourceAsStream(String name)