Java ClassLoader Load get(final String key, final String filename)

Here you can find the source of get(final String key, final String filename)

Description

get

License

Open Source License

Declaration

public static String get(final String key, final String filename) 

Method Source Code

//package com.java2s;
/**/*from w ww  . j av  a2  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.net.URL;
import java.util.Properties;

public class Main {
    public static String get(final String key, final String filename) {
        Properties props;
        try {
            props = load(new File(filename));
        } catch (final IOException e) {
            return "getting key " + key + " from file " + filename + " failed " + e;
        }

        return (props.getProperty(key));
        // return "'" + key + "' not found in " + filename;
    }

    /**
     * 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. getAbsoluteBasePath()
  2. getAbsolutePath(String file)
  3. getAbsolutePathEssenceDesc(String file)
  4. getAllArquillianLibs()