Java Properties Load from File load(File file)

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

Description

Loads a Map from a File assuming strings as keys and values.

License

Open Source License

Parameter

Parameter Description
file the File containing a Properties -like layout of keys and values.

Exception

Parameter Description
IOException if the file reader throws IOException.

Return

the read data as a .

Declaration

public static Map<String, String> load(File file) throws IOException 

Method Source Code

//package com.java2s;
/**//from w w w .ja v a  2  s.  c  om
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

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

import java.io.Reader;

import java.util.HashMap;

import java.util.Map;
import java.util.Properties;

public class Main {
    /**
     * Loads a {@link Map} from a {@link Reader} assuming strings as keys
     * and values.
     *
     * @param reader the {@link Reader} containing a {@link Properties}-like
     * layout of keys and values.
     * @return the read data as a {@link Map}.
     * @throws IOException if the {@code reader} throws {@link IOException}.
     */
    public static Map<String, String> load(Reader reader) throws IOException {
        Properties props = new Properties();
        props.load(reader);
        return new HashMap<String, String>((Map) props);
    }

    /**
     * Loads a {@link Map} from an {@link InputStream} assuming strings as keys
     * and values.
     *
     * @param stream the {@link InputStream} containing a
     * {@link Properties}-like layout of keys and values.
     * @return the read data as a {@link Map}.
     * @throws IOException if the {@code stream} throws {@link IOException}.
     */
    public static Map<String, String> load(InputStream stream) throws IOException {
        Properties props = new Properties();
        props.load(stream);
        return new HashMap<String, String>((Map) props);
    }

    /**
     * Loads a {@link Map} from a {@link File} assuming strings as keys
     * and values.
     *
     * @param file the {@link File} containing a {@link Properties}-like
     * layout of keys and values.
     * @return the read data as a {@link Map}.
     * @throws IOException if the file reader throws {@link IOException}.
     */
    public static Map<String, String> load(File file) throws IOException {
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(file);
            return load(stream);
        } finally {
            closeIfNotNull(stream);
        }
    }

    private static void closeIfNotNull(Closeable closeable) throws IOException {
        if (closeable != null)
            closeable.close();
    }
}

Related

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