Java Properties Load loadProperties(Properties props, final Class clazz, final String resourceName)

Here you can find the source of loadProperties(Properties props, final Class clazz, final String resourceName)

Description

Load properties from a resource.

License

Apache License

Parameter

Parameter Description
props The properties object to load.
clazz The class used to locate the resource.
resourceName The name of the resource.

Declaration

public static void loadProperties(Properties props, final Class clazz,
        final String resourceName) 

Method Source Code

//package com.java2s;
// Licensed to the Apache Software Foundation (ASF) under one or more contributor

import java.io.BufferedInputStream;

import java.io.IOException;

import java.util.Properties;

public class Main {
    /** Load properties from a resource.
     */*  w  ww .  j a v a2 s  .c o m*/
     * This loads properties from the given resource (relative to the given class)
     * into the given properties object.  If any error occurs, this method prints a
     * message to stderr but otherwise takes no special action.
     *
     * <p>You typically use this to merge properties for your application into the
     * System properties by calling this method as follows:
     * <pre>Utility.loadProperties(System.getProperties(), MyClass.class,
     * "MyClass.properties");</pre> This adds the properties from the file
     * <code>MyClass.properties</code> (which must exist in the same directory as the
     * file <code>MyClass.class</code>, whether in the filesystem or in a jar) into
     * the system properties.  You can then fetch these properties from anywhere in
     * your program with <code>System.getProperty(String)</code>,
     * <code>Integer.getInteger</code>, etc.
     *
     * <p>You can also use the method to load properties into a newly created
     * properties object that you provide.  This keeps your properties separate, but
     * then you have to pass a properties object all over the place, and you can't
     * take advantage of non-String properties through methods like
     * <code>Long#getLong(String)</code>, etc.
     *
     * @param props The properties object to load.
     * @param clazz The class used to locate the resource.
     * @param resourceName The name of the resource.
     */
    public static void loadProperties(Properties props, final Class clazz,
            final String resourceName) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(
                    clazz.getResourceAsStream(resourceName));
            props.load(in);
        } catch (IOException ex) {
            System.err.println("I/O exception while loading \""
                    + resourceName + "\": " + ex.getMessage());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignore) {
                }
            }
        }
    }
}

Related

  1. loadProperties(Class clazz)
  2. loadProperties(Class clazz, String name)
  3. loadProperties(Properties p, String progname)
  4. loadProperties(Properties properties, Class clazz, String propDir, String name)
  5. loadProperties(Properties properties, Class baseClass, String... propertiesName)
  6. loadProperties(String bucketName, String key)
  7. loadProperties(String conf)
  8. loadProperties(String name, ClassLoader loader)
  9. loadProperties(String properties)