@tsing1226
2016-11-17T19:20:52.000000Z
字数 2737
阅读 1598
java
在Java项目中,有很多场景需要读取配置文件的配置信息,比如properties文件。但是在Java项目中我们通常采用通过加载编译运行环境(target/class)下文件下加载,但是当项目打成jar包运行在Linux中就读取不到配置文件。本文介绍的是三个方法来读取properties文件。详细代码如下:
PropertiesUtil.java
package com.lakala.exices.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 属性 工具操作类
*
*/
public class PropertiesUtil {
static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
/**
* 这是从resources/conf下读取的文件,如果配置文件不在conf下而直接在resources下则去除conf/
*/
static String CONFIG_FILE = "conf/xxx.properties";
static Properties memProperties = null;
/**
* 设置配置文件
*
* @param fileName
*/
public static void setConfigFile(String fileName) {
if (StringUtils.isNotBlank(fileName)) {
PropertiesUtil.CONFIG_FILE = fileName;
}
}
/**
* 通过key获取配置值
*
* @param key
* @return
* @throws Exception
*/
public static String getProperty(String key) throws Exception {
return getProperty(key, null);
}
public static String getProperty(String key, String defaultVl) throws Exception {
if (null == memProperties) {
logger.info("The memProperties is empty ,read properties from " + CONFIG_FILE);
memProperties = loadPropertiesFromFile(null);
}
String value = memProperties.getProperty(key, defaultVl);
if (null == value) {
String info = "Cannot get valid value for the key:" + key + " from file " + CONFIG_FILE;
throw new Exception(info);
}
return value;
}
/**
* 读取配置文件
*
* @param fileName:配置文件名称
* @return
*/
protected static Properties loadPropertiesFromFile(String fileName) throws Exception {
if (fileName == null || fileName.isEmpty()) {
fileName = CONFIG_FILE;
}
memProperties = new Properties();
FileInputStream in = null;
try {
logger.info("propertyUtil.classLoader=" + PropertiesUtil.class.getClassLoader());
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);// 以数据流的方式加载
/**
* Method1:
*/
if (inputStream == null) {
inputStream = PropertiesUtil.class.getResourceAsStream(fileName);
}
/**
* Method2:
*/
if (inputStream == null) {
inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
}
/**
* Method3:
*/
if (inputStream == null) {
String localPath = new PropertiesUtil().getClass().getClassLoader().getResource(".").getPath();
int idx = localPath.indexOf(":") + 1;
localPath = localPath.substring(idx);
String filePathName = localPath + fileName;
inputStream = new FileInputStream(filePathName);
}
memProperties.load(inputStream);
} catch (Exception ex) {
logger.error("Error occured when reading the properties file:" + fileName, ex);
throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ex) {
//
}
in = null;
}
}
return memProperties;
}
public static void main(String[] args) throws Exception {
PropertiesUtil.setConfigFile("conf/manager.properties");
System.out.println(PropertiesUtil.getProperty("basePath"));
System.out.println(PropertiesUtil.getProperty("outFilePath"));
}
}