Java Properties Load from File load(Class cls)

Here you can find the source of load(Class cls)

Description

Load a Properties object for a specific class.

License

Open Source License

Parameter

Parameter Description
cls The class to load the properties for.

Exception

Parameter Description
IOException if an I/O error occurs during loading.

Return

The loaded properties.

Declaration

public static Properties load(Class<?> cls) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w  w  w .  ja  v  a 2  s .c o  m*/
 * Copyright (c) 2016-2017 Holger de Carne and contributors, All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Main {
    /**
     * Load a {@link Properties} object for a specific class.
     * <p>
     * This function assumes that the properties file is a resource named as the submitted class with the extension
     * .properties.
     *
     * @param cls The class to load the properties for.
     * @return The loaded properties.
     * @throws IOException if an I/O error occurs during loading.
     */
    public static Properties load(Class<?> cls) throws IOException {
        Properties properties;

        try (InputStream stream = cls.getResourceAsStream(cls.getSimpleName() + ".properties")) {
            if (stream == null) {
                throw new FileNotFoundException("Resource not found for class: " + cls.getName());
            }
            properties = new Properties();
            properties.load(stream);
        }
        return properties;
    }
}

Related

  1. load(boolean loadAsResourceBundle, String name, ClassLoader loader)
  2. load(Class type, String resource, Properties map)
  3. load(File f)
  4. load(File file)
  5. load(File file)
  6. load(File file)