Java Properties Load from File load(File propertiesFile)

Here you can find the source of load(File propertiesFile)

Description

Loads a properties file.

License

Open Source License

Parameter

Parameter Description
propertiesFile properties file to be loaded

Return

loaded properties, empty if properties file does not exist

Declaration

public static Properties load(File propertiesFile) 

Method Source Code


//package com.java2s;
/*/* ww  w.ja  v  a 2s.c  o m*/
 * Copyright (c) 2007-2013 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

import java.io.BufferedInputStream;

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

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;
import static com.google.common.base.Preconditions.checkNotNull;

public class Main {
    /**
     * Loads a properties file. If file does not exist creates a set of empty properties.
     *
     * @param propertiesFile properties file to be loaded
     * @return loaded properties, empty if properties file does not exist
     */
    public static Properties load(File propertiesFile) {
        if (!checkNotNull(propertiesFile).exists()) {
            return new Properties();
        }
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(propertiesFile));
            Properties properties = new Properties();
            if (propertiesFile.getName().endsWith(".xml")) {
                properties.loadFromXML(in);
            } else {
                properties.load(in);
            }
            return properties;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

Related

  1. load(File file)
  2. load(File file)
  3. load(File file)
  4. load(File file)
  5. load(File file, Properties data)
  6. load(final Class key, final String env, final Properties defaults)
  7. load(final Properties props, final Properties toLoad)
  8. load(final String folder, final String fileName)
  9. load(InputStream in)