Java Properties Load loadProperties(String conf)

Here you can find the source of loadProperties(String conf)

Description

load Properties

License

Open Source License

Declaration

public static Properties loadProperties(String conf) throws IOException 

Method Source Code

//package com.java2s;
/*//  w ww  . ja  va2s. c  o  m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import java.io.*;

import java.util.*;

public class Main {
    public static Properties loadProperties(String conf) throws IOException {
        Properties macros = new Properties();
        macros.setProperty("%BASE%", new File(conf).getAbsoluteFile().getParent());

        Properties p = new Properties();
        FileInputStream fin = new FileInputStream(conf);
        try {
            p.load(fin);
            for (Object o1 : p.keySet()) {
                String k1 = (String) o1;
                String v1 = p.getProperty(k1);
                Enumeration<Object> keys = macros.keys();
                while (keys.hasMoreElements()) {
                    String k2 = (String) keys.nextElement();
                    String v2 = macros.getProperty(k2);
                    v1 = v1.replaceAll(k2, v2);
                }
                p.put(k1, v1);
            }
        } finally {
            try {
                fin.close();
            } catch (IOException e) {
            }
        }
        return p;
    }

    public static String replaceAll(String source, String toReplace, String replacement) {
        int idx = source.lastIndexOf(toReplace);
        if (idx != -1) {
            StringBuffer ret = new StringBuffer(source);
            ret.replace(idx, idx + toReplace.length(), replacement);
            while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
                ret.replace(idx, idx + toReplace.length(), replacement);
            }
            source = ret.toString();
        }

        return source;
    }

    public static String toString(Collection<String> list) {
        return toString(list.toArray(new String[list.size()]));
    }

    public static String toString(Object array[]) {
        return toString(array, ",");
    }

    public static String toString(int array[]) {
        return toString(array, ",");
    }

    public static String toString(long array[]) {
        return toString(array, ",");
    }

    public static String toString(double[] array) {
        return toString(array, ",");
    }

    public static String toString(Object array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(Object array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > start)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(long array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(double array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(int array[], String sep) {
        return toString(array, sep, 0, array.length);
    }

    public static String toString(double array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(long array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String toString(int array[], String sep, int start, int end) {
        StringBuffer sb = new StringBuffer();
        for (int i = start; i < end; i++) {
            if (i > 0)
                sb.append(sep);
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. loadProperties(Properties p, String progname)
  2. loadProperties(Properties properties, Class clazz, String propDir, String name)
  3. loadProperties(Properties properties, Class baseClass, String... propertiesName)
  4. loadProperties(Properties props, final Class clazz, final String resourceName)
  5. loadProperties(String bucketName, String key)
  6. loadProperties(String name, ClassLoader loader)
  7. loadProperties(String properties)
  8. loadProperties(String properties)
  9. loadProperties(String propertyName)