Java Properties Load loadPropertiesFromResources(Class cls, String[] resources)

Here you can find the source of loadPropertiesFromResources(Class cls, String[] resources)

Description

load Properties From Resources

License

Apache License

Declaration

public static Properties[] loadPropertiesFromResources(Class<?> cls, String[] resources) throws IOException 

Method Source Code

//package com.java2s;
/*/*w w  w.j  a v a2 s.  c om*/
 * #%L
 * JavaUtil
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */

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

import java.util.Properties;

public class Main {
    public static Properties[] loadPropertiesFromResources(Class<?> cls, String[] resources) throws IOException {
        if (resources == null) {
            return null;
        }
        Properties[] result = new Properties[resources.length];
        for (int i = 0; i < resources.length; i++) {
            result[i] = loadPropertiesFromResource(cls, resources[i]);
        }
        return result;
    }

    public static Properties loadPropertiesFromResource(Class<?> cls, String resource) throws IOException {
        return createPropertiesFromResource(cls, resource);
    }

    /**
     * Creates a <code>Properties</code> objects and loads the data in the given
     * resource, using the given class' <code>getResourceAsStream</code> method.
     *
     * @param classObj the {@link Class} whose <code>getResourceAsStream</code>
     * method is called, cannot be <code>null</code>.
     * @param resourceName a resource {@link String}, visible * * from
     * <code>classObj</code>'s class loader. If <code>null</code>, this methods
     * returns an empty {@link Properties} object.
     * @return a <code>Properties</code> object.
     * @throws IOException
     */
    public static Properties createPropertiesFromResource(Class<?> classObj, String resourceName)
            throws IOException {
        Properties result = new Properties();
        readPropertiesFromResource(result, classObj, resourceName);
        return result;
    }

    /**
     * Loads properties from the given resource into the given properties
     * object.
     *
     * @param properties the {@link Properties}, using the given class'
     * <code>getResourceAsStream</code> method.
     * @param classObj the {@link Class} whose <code>getResourceAsStream</code>
     * method is called, cannot be <code>null</code>.
     * @param resourceName a resource {@link String}, visible * * from
     * <code>classObj</code>'s class loader. If <code>null</code>, this method
     * does nothing.
     * @throws IOException if an error occurred reading the resource.
     */
    public static void readPropertiesFromResource(Properties properties, Class<?> classObj, String resourceName)
            throws IOException {
        if (classObj == null) {
            throw new IllegalArgumentException("classObj cannot be null");
        }
        if (resourceName != null) {
            InputStream inputStream = classObj.getResourceAsStream(resourceName);
            if (inputStream != null) {
                try {
                    properties.load(inputStream);
                } finally {
                    inputStream.close();
                }
            } else {
                throw new IOException(resourceName + " not found.");
            }
        }
    }
}

Related

  1. loadProperties(String resource)
  2. loadProperties(String resourceName)
  3. loadProperties(String resourceName, ClassLoader classLoader)
  4. loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)
  5. loadPropertiesFromResource(String name)
  6. loadPropertiesFromString(String propertiesString)
  7. loadPropertiesResources(String name)
  8. loadSystemPropertiesFromPropertiesResource(String propertiesResourceName)