Java URL Load readPropertyFile(URL url)

Here you can find the source of readPropertyFile(URL url)

Description

This class reads a properties file and replaces constructs surrounded by ${ and } by a system properties specified within the curly brackets.

License

Apache License

Parameter

Parameter Description
url is the url to be read.

Exception

Parameter Description
IOException is thrown in cases of IO failures.

Return

Returns a Properties object containing the properties.

Declaration

public static Properties readPropertyFile(URL url) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**/*from  ww w  .  j  ava 2s  . c  om*/
     * This class reads a properties file and replaces constructs surrounded by
     * ${ and } by a system properties specified within the curly brackets. For
     * example, the construct ${user.home} is replaced by the used home
     * directory.
     * 
     * @param url
     *            is the url to be read.
     * @return Returns a Properties object containing the properties.
     * @throws IOException
     *             is thrown in cases of IO failures.
     */
    public static Properties readPropertyFile(URL url) throws IOException {
        if (url == null) {
            throw new IllegalArgumentException("URL must not be null.");
        }
        try (InputStream inputStream = url.openStream()) {
            Properties properties = new Properties();
            properties.load(inputStream);
            Pattern pattern = Pattern.compile("\\$\\{([\\w.]+)\\}");
            for (Entry<Object, Object> entry : properties.entrySet()) {
                Object key = entry.getKey();
                String value = (String) entry.getValue();
                while (true) {
                    Matcher matcher = pattern.matcher(value);
                    if (!matcher.find()) {
                        break;
                    }
                    String string = matcher.group(1);
                    String propertyValue = System.getProperty(string);
                    value = value.replaceAll("\\$\\{" + string + "\\}", propertyValue);
                }
                properties.put(key, value);
            }
            return properties;
        }
    }
}

Related

  1. readPage(final String urlStr)
  2. readPage(URL url)
  3. readProperties(final URL url)
  4. readProperties(final URL url)
  5. readProperties(URL resource)
  6. readResource(final URL filename)
  7. readResource(URL resource)
  8. readServicesFromUrl(Collection list, URL url)
  9. readSqlStatements(URL url)