Here you can find the source of getResourceAsStream(String filename)
Parameter | Description |
---|---|
filename | a parameter |
public static InputStream getResourceAsStream(String filename)
//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; } } }