Java Properties Load from File loadProperties(File file)

Here you can find the source of loadProperties(File file)

Description

Load properties from a File.

License

Open Source License

Parameter

Parameter Description
file Propertie file to load.

Exception

Parameter Description
IOException an exception

Return

The loaded Properties.

Declaration

public static Properties loadProperties(File file) throws IOException 

Method Source Code

//package com.java2s;
/*  =====================================================================
 *  Mfg_scmUtils -/*from  ww w.  j a v a  2  s  . c  o m*/

 -------------------------------------------------------------------------
 Copyright (c) 1991-2013 Andre Charles Legendre <andre.legendre@kalimasystems.org>
 Copyright other contributors as noted in the AUTHORS file.

 This file is part of JSPluginManager, the Javascript pluginManager for java

 This 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 software 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.util.Iterator;

import java.util.Properties;

public class Main {
    /**
     * Load properties from a <code>File</code>.
     * 
     * @param file
     *            Propertie file to load.
     * @return The loaded Properties.
     * @throws IOException
     */
    public static Properties loadProperties(File file) throws IOException {
        if (file.exists()) {
            return loadProperties(new FileInputStream(file));
        }
        return null;
    }

    /**
     * Load properties from an <code>InputStream</code>.
     * 
     * @param is
     *            InputStream from which load properties.
     * @return The loaded Properties.
     * @throws IOException
     */
    public static Properties loadProperties(InputStream is)
            throws IOException {
        Properties properties = new Properties();
        try {
            properties.load(is);
            for (Iterator<Object> i = properties.keySet().iterator(); i
                    .hasNext();) {
                String property = (String) i.next();
                properties.setProperty(property,
                        properties.getProperty(property).trim());
            }
        } catch (IOException e) {
            // ignore
            throw e;
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (final IOException e) {
                throw e;
            }
        }
        return properties;
    }
}

Related

  1. loadParamFromFile(String fileName, String param, String defValue)
  2. loadParams(String fileName)
  3. loadPriority()
  4. loadProperties(File directory, String propertiesFileName)
  5. loadProperties(File f)
  6. loadProperties(File file)
  7. loadProperties(File file)
  8. loadProperties(File file)
  9. loadProperties(File file)