Java Resource File loadConfigFile(String resource, Class clazz)

Here you can find the source of loadConfigFile(String resource, Class clazz)

Description

load Config File

License

Open Source License

Declaration

public static InputStream loadConfigFile(String resource, Class<?> clazz) 

Method Source Code

//package com.java2s;
/**//from   w ww  .  j av a2s  . c o  m
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;

import java.net.URL;

public class Main {
    public static InputStream loadConfigFile(String resource, Class<?> clazz) {
        ClassLoader classLoader = null;
        try {
            Method method = Thread.class.getMethod("getContextClassLoader");
            classLoader = (ClassLoader) method.invoke(Thread.currentThread());
        } catch (Exception e) {
            System.out.println("loadConfigFile error: ");
            e.printStackTrace();
        }
        if (classLoader == null) {
            classLoader = clazz.getClassLoader();
        }
        try {
            if (classLoader != null) {
                URL url = classLoader.getResource(resource);
                if (url == null) {
                    System.out.println("Can not find resource:" + resource);
                    return null;
                }
                if (url.toString().startsWith("jar:file:")) {
                    System.out.println("Get resource \"" + resource + "\" from jar:\t" + url.toString());
                    return clazz.getResourceAsStream(resource.startsWith("/") ? resource : "/" + resource);
                } else {
                    System.out.println("Get resource \"" + resource + "\" from:\t" + url.toString());
                    return new FileInputStream(new File(url.toURI()));
                }
            }
        } catch (Exception e) {
            System.out.println("loadConfigFile error: ");
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getStreamFromResource(Class clazz, String resourceName)
  2. getTestResourceFile(String pName, Class pClass)
  3. inputStream(Class baseClass, String resourceName)
  4. isFullIRI(String resource)
  5. loadAutoConfigFiles(ClassLoader classLoader, String resourceName)
  6. loadUTF8(String resource)
  7. openInputStream(String resourceString, ClassLoader classLoader)
  8. parseASX(String inputResource)
  9. printUsage(ResourceBundle bundle)