Java Class Loader loadProperties(final Properties properties, final String fileName, final ClassLoader cl)

Here you can find the source of loadProperties(final Properties properties, final String fileName, final ClassLoader cl)

Description

Load properties file fileName and put it properties into properties instance.

License

Open Source License

Parameter

Parameter Description
properties a parameter
fileName a parameter
cl a parameter

Declaration

public static void loadProperties(final Properties properties, final String fileName, final ClassLoader cl) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Martin Lippert and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from w  ww.  j ava 2s .c o m
 * Contributors:
 *   Martin Lippert                   initial implementation      
 *   Angelo ZERR                      manage springweaver into Jpa context
 *******************************************************************************/

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class Main {
    /**
     * Load properties file fileName and put it properties into properties
     * instance.
     * 
     * @param properties
     * @param fileName
     * @param cl
     */
    public static void loadProperties(final Properties properties, final String fileName, final ClassLoader cl) {

        // Loop for fileName properties file
        Enumeration<URL> urlProperties = null;
        try {
            urlProperties = cl == null ? ClassLoader.getSystemResources(fileName) : cl.getResources(fileName);
        } catch (IOException e) {
            return;
        }

        Properties fileProperties = new Properties();
        while (urlProperties.hasMoreElements()) {
            URL url = urlProperties.nextElement();
            try {
                if (url != null) {
                    // Load properties file
                    fileProperties.load(url.openStream());
                    // merge properties file lodead into the properties.o
                    properties.putAll(fileProperties);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. loadConfig(String path, ClassLoader classLoader)
  2. loadForClass(final Class clazz, final String filename)
  3. loadManifest(Class manifestFileClass)
  4. loadManifest(Class theClass)
  5. loadManifestFrom(Class c)
  6. loadStyleSheet(Class type)
  7. loadTextFile(Class relToThisClass, String relFileName)
  8. pathFromLoaders(final Class clazz)
  9. print(ClassLoader loader)