Java URL Load loadPropertiesFromFileOrURL(String propertiesFile)

Here you can find the source of loadPropertiesFromFileOrURL(String propertiesFile)

Description

Create a java properties object from a location.

License

Open Source License

Parameter

Parameter Description
propertiesFile can be a file path or a url.

Exception

Parameter Description
IOException an exception

Return

The loaded properties file.

Declaration

public static Properties loadPropertiesFromFileOrURL(String propertiesFile) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w w  w  . j a va 2  s . com
 * Copyright: (c) 2004-2010 Mayo Foundation for Medical Education and 
 * Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, or as used to identify 
 * MFMER as the author of this software, the trade names, trademarks, service
 * marks, or product names of the copyright holder shall not be used in
 * advertising, promotion or otherwise in connection with this software without
 * prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/legal/epl-v10.html
 * 
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

public class Main {
    public static String propertiesLocationKey = "_propertiesFileLocation_";
    public static String propertiesParentFolderKey = "_propertiesFileParentFolder_";

    /**
     * Create a java properties object from a location. Adds a
     * "propertiesFileParentFolder" value to the properties object. The value is
     * set to the file path of the folder containing the properties file. This
     * is useful for the method
     * Log4JUtility.configureLog4JFromPathSpecifiedInProperties
     * 
     * @param propertiesFile
     *            can be a file path or a url.
     * @return The loaded properties file.
     * @throws IOException
     */
    public static Properties loadPropertiesFromFileOrURL(String propertiesFile) throws IOException {
        if (propertiesFile == null || propertiesFile.length() == 0) {
            throw new IOException("No file provided");
        }
        InputStream inputStream = null;

        String location;
        String parentFolder;

        try {
            URL temp = new URL(propertiesFile);
            inputStream = temp.openStream();
            location = temp.toString();
            parentFolder = temp.toString();
            int i = parentFolder.lastIndexOf("/");
            if (i == -1) {
                i = parentFolder.lastIndexOf("\\");
            }
            parentFolder = parentFolder.substring(0, i);
        } catch (MalformedURLException e) // Its not a url, must be a file name
        {
            File inputFile = new File(propertiesFile);
            inputStream = new FileInputStream(inputFile);
            parentFolder = inputFile.getParentFile().getAbsolutePath();
            location = inputFile.getAbsolutePath();
        }
        Properties props = new Properties();
        props.load(inputStream);
        inputStream.close();
        props.put(propertiesLocationKey, location);
        props.put(propertiesParentFolderKey, parentFolder);

        return props;
    }
}

Related

  1. loadObject(URL url)
  2. loadProperties(final URL url)
  3. loadProperties(Properties p, URL url)
  4. loadProperties(URL url)
  5. loadProperties(URL url)
  6. loadPropertiesFromURL(URL url)
  7. loadProps(URL url, Properties props)
  8. loadRemoteJSON(final URL source)
  9. loadText(URL url, boolean allowCache)