java读取配置文件内容

时间:2022-07-22 22:33:26

利用com.typesafe.config包实现

<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.0.2</version>
</dependency>

被读取的配置文件config.properties:

patrol.interval=5
push.interval=30
data = [{ controlNum : 1, loopNum : 1, pointNum : 1, status : 110 },\
{ controlNum : 1, loopNum : 1, pointNum : 1, status = 111 }]

java 工具类:

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; public class ServerConfig { private final static Config config; static {
config = ConfigFactory.load("config.properties");
} public static Config load() {
return config;
}
}

调用方式:

Config serverConfig = ServerConfig.load();
int PATROL_INTERVAL = serverConfig.getInt("patrol.interval");

或者使用Java IO实现

public class PropertiesReader {

    private static final String PROPERTIES_FILE_NAME = "aa.properties";

    private static Properties config = null;

    static {
config = readProperties(PROPERTIES_FILE_NAME);
} public static String getProperty(String key){
return config.getProperty(key);
} private static Properties readProperties(String fileName){
InputStream inputStream = WsPropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
} public static Properties readProperties(InputStream inputStream){
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}

对于配置文件中有嵌套的配置内容,一种方法是,在Spring环境中

使用EmbeddedValueResolverAware读取配置文件内容

另外可以自己解析${}的形式

public class PropertiesReader {

    private static final String COMMON_PROPERTIES_FILE_NAME = "common-constants.properties";
private static final String WEB_PROPERTIES_FILE_NAME = "application.properties"; private static Properties commonConfig = null;
private static Properties webConfig = null; static {
commonConfig = readProperties(COMMON_PROPERTIES_FILE_NAME);
webConfig = readProperties(WEB_PROPERTIES_FILE_NAME);
} public static String getProperty(String key) {
return commonConfig.getProperty(key);
} /**
* 支持复杂el表达式递归调用
* @param key
* @return
*/
public static String getWebProperty(String key) {
String value = webConfig.getProperty(key);
String regex = "\\$\\{(.*?)\\}"; //正则表达式
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
while(matcher.find()){
String subProperty = getWebProperty(matcher.group(1));
value = value.replace("${"+matcher.group(1)+"}",subProperty);
}
return value;
} private static Properties readProperties(String fileName) {
InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream(fileName); Properties config = new Properties();
try {
config.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}