Get properties from an input stream. - Java java.util

Java examples for java.util:Properties File

Description

Get properties from an input stream.

Demo Code

/*//from ww  w .j av  a2  s .c  o  m
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
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 {
    /**
     * Get properties from an input stream.
     *
     * @param  is The input stream.
     *
     * @return The properties loaded from the input stream.
     *
     * @throws IOException If the is an I/O error reading from the stream.
     */
    public static Properties getProperties(InputStream is)
            throws IOException {
        /*log.fine("getProperties(InputStream): called");*/

        // Create properties object laoded from input stream
        Properties properties = new Properties();

        properties.load(is);

        return properties;
    }

    /**
     * Get properties from a file.
     *
     * @param  file The file.
     *
     * @return The properties loaded from the file.
     *
     * @throws IOException If there is an I/O error reading from the file.
     */
    public static Properties getProperties(File file) throws IOException {
        /*log.fine("getProperties(File): called");*/

        // Open the file as an input stream
        InputStream is = new FileInputStream(file);

        // Create properties object loaded from the stream
        Properties properties = getProperties(is);

        // Close the file
        is.close();

        return properties;
    }

    /**
     * Get properties from a url.
     *
     * @param  url The URL.
     *
     * @return The properties loaded from the url.
     *
     * @throws IOException If there is an I/O error reading from the URL.
     */
    public static Properties getProperties(URL url) throws IOException {
        /*log.fine("getProperties(URL): called");*/

        // Open the URL as an input stream
        InputStream is = url.openStream();

        // Create properties object loaded from the stream
        Properties properties = getProperties(is);

        // Close the url
        is.close();

        return properties;
    }

    /**
     * Get properties from a path name. The path name may refer to either a file or a URL.
     *
     * @param  pathname The path name.
     *
     * @return The properties loaded from the file or URL.
     *
     * @throws IOException If there is an I/O error reading from the URL or file named by the path.
     */
    public static Properties getProperties(String pathname)
            throws IOException {
        /*log.fine("getProperties(String): called");*/

        // Check that the path is not null
        if (pathname == null) {
            return null;
        }

        // Check if the path is a URL
        if (isURL(pathname)) {
            // The path is a URL
            return getProperties(new URL(pathname));
        } else {
            // Assume the path is a file name
            return getProperties(new File(pathname));
        }
    }

    /**
     * Helper method. Guesses whether a string is a URL or not. A String is considered to be a url if it begins with
     * http:, ftp:, or uucp:.
     *
     * @param  name The string to test for being a URL.
     *
     * @return True if the string is a URL and false if not.
     */
    private static boolean isURL(String name) {
        return (name.toLowerCase().startsWith("http:")
                || name.toLowerCase().startsWith("ftp:") || name
                .toLowerCase().startsWith("uucp:"));
    }
}

Related Tutorials