Java Class Loader loadConfig(String path, ClassLoader classLoader)

Here you can find the source of loadConfig(String path, ClassLoader classLoader)

Description

Opens input stream to the specified path, which can be a class resources resolved using the provided class loader, a url or a file

License

Open Source License

Parameter

Parameter Description
path path to the config, which is a class resources or a url or a file

Return

input stream or null if the path wasn't recognized

Declaration

public static InputStream loadConfig(String path, ClassLoader classLoader) 

Method Source Code


//package com.java2s;

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

import java.net.URL;

import static java.lang.Thread.currentThread;

public class Main {
    /**/*from  ww  w . jav  a 2 s  . c  o  m*/
     * Opens input stream to the specified path, which can be a class resources resolved using the context class loader,
     * a url or a file
     *
     * @param path path to the config, which is a class resources or a url or a file
     * @return input stream or null if the path wasn't recognized
     */
    public static InputStream loadConfig(String path) {
        return loadConfig(path, currentThread().getContextClassLoader());
    }

    /**
     * Opens input stream to the specified path, which can be a class resources resolved using the provided class
     * loader, a url or a file
     *
     * @param path path to the config, which is a class resources or a url or a file
     * @return input stream or null if the path wasn't recognized
     */
    public static InputStream loadConfig(String path, ClassLoader classLoader) {
        InputStream input = null;
        URL resource = classLoader.getResource(path);
        if (resource != null) {
            try {
                input = resource.openStream();
            } catch (IOException exception) {
                // it's not a class
            }
        }
        if (input == null) {
            try {
                input = new URL(path).openStream();
            } catch (IOException exception) {
                // it's not an url
            }
        }
        if (input == null) {
            try {
                input = new FileInputStream(path);
            } catch (IOException exception) {
                // it's not a file
            }
        }
        return input;
    }
}

Related

  1. loadClass(String name)
  2. loadClass(String name, Object fallbackClass)
  3. loadClasses(Collection filenames, String packageName, File outputDir)
  4. loadClassesImplementsTheInterface(String packageName, Class interfaceClazz)
  5. loadClassifiers(String dataDirectoryName)
  6. loadForClass(final Class clazz, final String filename)
  7. loadManifest(Class manifestFileClass)
  8. loadManifest(Class theClass)
  9. loadManifestFrom(Class c)