Here you can find the source of loadPropertiesFromFileOrURL(String propertiesFile)
Parameter | Description |
---|---|
propertiesFile | can be a file path or a url. |
Parameter | Description |
---|---|
IOException | an exception |
public static Properties loadPropertiesFromFileOrURL(String propertiesFile) throws IOException
//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; } }