Here you can find the source of loadForClass(final Class> clazz, final String filename)
public static Properties loadForClass(final Class<?> clazz, final String filename)
//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; } }