Java Resource Load getResource(String path)

Here you can find the source of getResource(String path)

Description

Get a resource relative to the application class.

License

Open Source License

Declaration

static InputStream getResource(String path) 

Method Source Code

//package com.java2s;
/*/*from   w w  w  . j  a v a2  s  . c om*/
 * Copyright 2013 John Smith
 *
 * This file is part of Willow.
 *
 * Willow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Willow 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Willow. If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact details: http://jewelsea.wordpress.com
 */

import java.io.*;

public class Main {
    /**
     * Get a resource relative to the application class.
     */
    static InputStream getResource(String path) {

        //System.out.println( ResourceUtil.class.getResource("../../../../../") );;
        //return ClassLoader.getSystemResource("../resources/org/jewelsea/willow/" + path).toExternalForm();

        InputStream x = ClassLoader.getSystemResourceAsStream("./" + path);
        if (x != null)
            return x;

        try {
            return new FileInputStream("./data/" + path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //return new File("./src/resources/" + path);
        return null;
    }
}

Related

  1. getResource(final String aResName)
  2. getResource(final String resource)
  3. getResource(String fileName)
  4. getResource(String fileName, int type)
  5. getResource(String name)
  6. getResource(String propertiesPath)
  7. getResource(String uri)
  8. getResourceAbsolutePath(String pluginId, String... path)
  9. getResourceAsBufferedReader(Class clazz, String resourceName)