使用java系统属性user.dir读取配置文件
2018-08-03 14:26
821 查看
适用于windows和linux服务器读取配置文件
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
public final class PropertyUtil {
private static Logger LOG = Logger.getLogger(PropertyUtil.class);
// 配置文件 private static Properties demoProps = new Properties(); // 单例模式 private static PropertyUtil instance = null; private PropertyUtil() { // user.dir为应用目录 String filePath = System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "resources" + File.separator; LOG.info(filePath); try { File demoFile = new File(filePath + "demo.properties"); if (httpFile.exists()) { demoProps.load(new FileInputStream(filePath + "demo.properties")); } } catch (IOException e) { LOG.info("The Exception occured.", e); } } public synchronized static PropertyUtil getInstance() { if (null == instance) { instance = new PropertyUtil(); } return instance; } /** * 获取参数值 * * @param key * properites的key值 * @param defValue * 默认值 * @return */ public String getValues(String key, String defValue) { String rtValue = null; if (null == key) { LOG.error("key is null"); } else { rtValue = getPropertiesValue(key); } if (null == rtValue) { LOG.warn("PropertyUtil.getValues return null, key is " + key); rtValue = defValue; } LOG.info("PropertyUtil.getValues: key is " + key + "; Value is " + rtValue); return rtValue; } /** * 根据key值获取server.properties的值 * * @param key * @return */ private String getPropertiesValue(String key) { String rtValue = demoProps.getProperty(key); return rtValue; }
}
相关文章推荐
- Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
- 使用java程序读取配置文件中的相关属性值-asp.net关注
- java.util.ResourceBundle使用详解(读取资源属性文件)
- Hibernate从Java属性文件(properties)读取配置信息
- java读取和设置配置文件中的属性参数
- 【新手入门】Python下不喜欢使用ConfigParser去读取配置文件的Java developer
- 使用java读取配置文件(ini、properties)
- Java使用properties读取配置文件
- JAVA 读取属性配置文件
- java 读取系统配置文件
- Java中读取properties属性配置文件内容方法
- java读取属性配置文件工具类
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- Spring 从文件系统读取配置文件, 使用@PropertySource
- 根据不同的操作系统读取配置文件/java读取属性文件代码
- 在web和非web环境都可以使用的属性文件属性读取java代码
- JAVA日志文件的使用与配置文件的读取
- Win7 :user profile server 您已经使用系统的默认配置文件登录
- Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库
- 在Java中使用Jdom读取xml配置文件