Java Formatter Usage getPropertiesString(Properties myProps)

Here you can find the source of getPropertiesString(Properties myProps)

Description

get Properties String

License

Apache License

Declaration

public static String getPropertiesString(Properties myProps) 

Method Source Code

//package com.java2s;
/*//  w  w w . j  av a2s  . c  o  m
 * Copyright 2009 the original author or authors.
 * Copyright 2009 SorcerSoft.org.
 *  
 * 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.
 */

import java.util.Enumeration;
import java.util.Formatter;

import java.util.Properties;

public class Main {
    public static String getPropertiesString(Properties myProps) {

        int maxKeySize = 0;
        int maxValueSize = 0;
        Enumeration<Object> keys = myProps.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = myProps.getProperty(key);
            if (key.length() > maxKeySize)
                maxKeySize = key.length();
            if (value.length() > maxValueSize)
                maxValueSize = value.length();
        }

        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb);
        // String format = "%" + maxKeySize + "s = %" + maxValueSize + "s\n";
        String format = "%-" + maxKeySize + "s = %s\n";
        Enumeration<Object> keys2 = myProps.keys();
        if (!keys2.hasMoreElements())
            sb.append("*** no properties to print ***\n");
        while (keys2.hasMoreElements()) {
            String key = (String) keys2.nextElement();
            String value = myProps.getProperty(key);
            formatter.format(format, key, value);
        }
        return sb.toString();
    }
}

Related

  1. formatZeroDecimals(Number x)
  2. fperfdata(String label, double val, String uom, int warnp, double warn, int critp, double crit, boolean minp, double minv, boolean maxp, double maxv)
  3. generateMacOnIncrease(final String baseMac, final long l)
  4. getCommitCounterStr(final long commitCounter)
  5. getFormatedTime(double timeInSeconds)
  6. GetRawView(String string)
  7. getRoundedString(double value, int powerOf10)
  8. incrementAlpha(String suppliedPrefix, String lastNumber, int numberLength)
  9. listToString(List list, String separator)