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 获取properties配置文件属性值
最新推荐文章于 2024-01-19 14:15:38 发布
本文介绍了一个用于读取配置文件的Java工具类——TemplateUtil。该工具采用单例模式,能够从指定路径加载.properties配置文件,并提供获取配置项的方法。文章详细展示了如何初始化该工具类及使用方法。
541

被折叠的 条评论
为什么被折叠?



