Java Class Loader loadForClass(final Class clazz, final String filename)

Here you can find the source of loadForClass(final Class clazz, final String filename)

Description

load For Class

License

Open Source License

Declaration

public static Properties loadForClass(final Class<?> clazz, final String filename) 

Method Source Code

//package com.java2s;
/**//from ww w  .j a  va  2 s. c o  m
   * Copyright (C) 2010 Swiss Library for the Blind, Visually Impaired and Print Disabled
   *
   * This file is part of dtbook-preptools.
   *    
   * dtbook-preptools is free software: you can redistribute it
   * and/or modify it under the terms of the GNU Lesser 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
   * Lesser General Public License for more details.
   *    
   * You should have received a copy of the GNU Lesser General Public
   * License along with this program. If not, see
   * <http://www.gnu.org/licenses/>.
   */

import java.io.File;
import java.io.FileInputStream;

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

public class Main {
    public static Properties loadForClass(final Class<?> clazz, final String filename) {
        final Properties props = new Properties();
        final InputStream in = clazz.getClassLoader().getResourceAsStream(filename);
        if (in != null) {
            try {
                props.load(in);
            } catch (IOException e) {
                e.printStackTrace();
                props.put("stamp", "loading from " + filename + "failed: " + e);
            }
        } else {
            props.put("stamp", "loading from " + filename + " failed!");

        }

        return props;
    }

    /**
     * Load a properties file from the classpath
     * 
     * @param filename
     * @return Properties
     * @throws IOException
     */
    public static Properties load(final String filename) {
        final Properties props = new Properties();
        final URL url = ClassLoader.getSystemResource(filename);
        if (url != null) {
            try {
                props.load(url.openStream());
            } catch (final IOException e) {
                e.printStackTrace();
                props.put("stamp", "loading from url " + url + " for " + filename + " failed " + e);
            }
        } else {
            props.put("stamp", "url was null for " + filename);
        }
        return props;
    }

    /**
     * Load a Properties File
     * 
     * @param propsFile
     * @return Properties
     * @throws IOException
     */
    public static Properties load(final File propsFile) throws IOException {
        final Properties props = new Properties();
        final FileInputStream fis = new FileInputStream(propsFile);
        props.load(fis);
        fis.close();
        return props;
    }
}

Related

  1. loadClass(String name, Object fallbackClass)
  2. loadClasses(Collection filenames, String packageName, File outputDir)
  3. loadClassesImplementsTheInterface(String packageName, Class interfaceClazz)
  4. loadClassifiers(String dataDirectoryName)
  5. loadConfig(String path, ClassLoader classLoader)
  6. loadManifest(Class manifestFileClass)
  7. loadManifest(Class theClass)
  8. loadManifestFrom(Class c)
  9. loadProperties(final Properties properties, final String fileName, final ClassLoader cl)