Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.cantaa.util;
  import java.util.Arrays;
  import java.util.List;
 import java.util.Set;
Load properties for different environments.

Hans Lesmeister
 public class PropertiesLoader {
     private static final Logger log = LoggerFactory.getLogger(PropertiesLoader.class);
     public static Properties loadProperties(String[] propResourceNames) {
         return loadProperties(Arrays.asList(propResourceNames));
     public static Properties loadProperties(Iterable<StringpropResourceNames) {
         Properties properties = new Properties();
         for (String propFile : propResourceNames) {
         return properties;
     private static void loadPropertiesFromResources(String propFileProperties targetProperties) {
         if (propFile.startsWith("classpath:")) {
             propFile = propFile.replace("classpath:""");
         InputStream in = PropertiesLoader.class.getResourceAsStream("/" + propFile);
         if (in == null) {
             // Properties-File does not exist. Ignore
             .warn("Properties File {} does not exist."propFile);
         .info("Loading properties from file: {}"propFile);

Loads properties from the stream and adds them to the applied target properties. The input stream is closed after reading the properties so the caller does not need to take care about that.

in Stream to read from. May not be null, the caller must take care that the stream is not null
     public static void loadPropertiesFromStream(InputStream inProperties targetProperties) {
         try {
             Properties properties = new Properties();
         } catch (IOException e) {
             throw new RuntimeException(e);
         } finally {

Write the contents of a properties instance to the log

props Properties to log
     public static void logProperties(Properties props) {
         .info("*** Configured Properties ***");
         List<Stringlist = getPropertiesAsList(props);
         for (String s : list) {
     public static List<StringgetPropertiesAsList(Properties properties) {
         List<Stringlist = new ArrayList<String>(properties.size());
         Enumeration en = properties.propertyNames();
         while (en.hasMoreElements()) {
             String property = (Stringen.nextElement();
             String value = property.toLowerCase().contains("password") ? "*****" : properties.getProperty(property);
             list.add(property + " = " + value);
         return list;
    public static Properties stripProperties(String prefixProperties properties) {
        return stripProperties(prefixpropertiestruenull);
    public static Properties stripProperties(String prefixProperties propertiesboolean rewrite) {
        return stripProperties(prefixpropertiesrewritenull);

Strips a prefix from properties

prefix Prefix to strip
sourceProps Properties to strip
rewrite if true (default) then properties with stripped names overwrite back to the source properties
separator is added to the prefix (Default is a dot.)
new Properties-instance with the stripped properties only

     Properties props = new Properties();
     props.setProperty("", "Some Value");  // Separated by a dot
     stripProperties("check", props);               // Without the dot
     log.debug(prop.getProperty("uno"));            // ==> "Some Value"
     log.debug(prop.getProperty(""));      // ==> "Some Value" => Original prop with prefix is NOT removed
    public static Properties stripProperties(String prefixProperties sourcePropsboolean rewriteString separator) {
        if (StringUtil.isEmpty(prefix) || (sourceProps == null)) {
            return null;
        String localPrefix = prefix + (separator == null ? "."separator);
        Properties strippedProps = new Properties();
        Set<Stringnames = sourceProps.stringPropertyNames();
        for (String name : names) {
            if (name.startsWith(localPrefix)) {
                String value = sourceProps.getProperty(name);
                String newName = name.substring(localPrefix.length());
                if (rewrite) {
        return strippedProps;
New to GrepCode? Check out our FAQ X