Java Resource Load getResourceAsProperties(String resource)

Here you can find the source of getResourceAsProperties(String resource)

Description

Resource as Properties

License

Open Source License

Parameter

Parameter Description
resource Resource

Declaration

public static Properties getResourceAsProperties(String resource) 

Method Source Code

//package com.java2s;
/**//from  www .j  av  a 2 s  . c  o  m
 * Copyright (c) 2016 eBay Software Foundation. All rights reserved.
 *
 * Licensed under the MIT license.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

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

import java.util.*;

public class Main {
    /**
     * Resource as Properties
     *
     * @param resource Resource
     */
    public static Properties getResourceAsProperties(String resource) {
        return getResourceAsProperties(Thread.currentThread().getContextClassLoader(), resource);
    }

    /**
     * Resource as Properties
     *
     * @param classLoader Class Loader
     * @param resource Resource
     */
    public static Properties getResourceAsProperties(ClassLoader classLoader, String resource) {
        if (classLoader == null) {
            classLoader = ClassLoader.getSystemClassLoader();
        }
        Properties properties = new Properties();
        InputStream inputStream = classLoader.getResourceAsStream(resource);
        if (inputStream == null) {
            throw new IllegalStateException("No such resource:" + resource);
        }
        try {
            properties.load(inputStream);
            return properties;
        } catch (IOException ex) {
            throw new IllegalStateException("Loading properties exception:", ex);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

Related

  1. getResourceAsFile(String path)
  2. getResourceAsFile(String resourceName)
  3. getResourceAsFile(String resourcePath)
  4. getResourceAsFile(T testSuite, String resourceName)
  5. getResourceAsInputStream(Class resourceClass, String resourceName)
  6. getResourceAsReader(final Class classLocation, final String resourceName)
  7. getResourceAsReader(String name)
  8. getResourceAsReader(String relativeName, Class clazz, String enc)
  9. getResourceAsStream(Class baseclass, String name)