Java Properties Load loadPropertiesResources(String name)

Here you can find the source of loadPropertiesResources(String name)

Description

Loads properties from all resource with the given name.

License

Apache License

Parameter

Parameter Description
name the resource name

Exception

Parameter Description

Return

properties loaded from all found resources

Declaration

public static Properties loadPropertiesResources(String name) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww  w  .  j  a va 2  s  .co m
 * Copyright 2014-2018 JKOOL, LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.IOException;
import java.io.InputStream;

import java.net.URL;

import java.util.Enumeration;

import java.util.Properties;

public class Main {
    /**
     * Loads properties from all resource with the given name.
     *
     * @param name
     *            the resource name
     * @return properties loaded from all found resources
     * @throws java.io.IOException
     *             if an error occurred when reading properties file
     *
     * @see java.lang.ClassLoader#getResources(String)
     * @see java.util.Properties#load(java.io.InputStream)
     */
    public static Properties loadPropertiesResources(String name) throws IOException {
        Properties rProps = new Properties();

        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Enumeration<URL> rEnum = loader.getResources(name);

        while (rEnum.hasMoreElements()) {
            try (InputStream ins = rEnum.nextElement().openStream()) {
                rProps.load(ins);
            }
        }

        return rProps;
    }
}

Related

  1. loadPropertiesFromResource(String name)
  2. loadPropertiesFromResources(Class cls, String[] resources)
  3. loadPropertiesFromString(String propertiesString)
  4. loadProperties(String resourceName, ClassLoader classLoader)
  5. loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)
  6. loadSystemPropertiesFromPropertiesResource(String propertiesResourceName)