java 获取properties配置文件属性值

package org.yingmm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @ClassName: TemplateUtil
 * @Date:2015-1-14 下午12:06:46
 * @author yingmm
 * @description: 读取配置文件类
 */
public class TemplateUtil {

	private static Properties prop;
	private static TemplateUtil tu = new TemplateUtil();

	/** 文件名称 */
	private String proFileStr = "template";

	public TemplateUtil() {
		String templatePath = System.getProperty("user.dir") + File.separator + "src/" + proFileStr + ".properties";
		if (prop == null) {
			prop = new Properties();
			File f = new File(templatePath);
			FileInputStream in = null;
			try {
				in = new FileInputStream(f);
				prop.load(in);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static synchronized TemplateUtil getTemplateUtil() {
		if (prop == null) {
			tu = new TemplateUtil();
		}
		return tu;
	}

	public String getProperties(String key, String defaultVal) {
		return prop.getProperty(key) == null ? defaultVal : prop.getProperty(key);
	}

}

你可能感兴趣的:(java)