java - 如何在 Java 中加载和解析(ant 风格)属性文件?
问题描述
如何将属性文件加载到 Java 中的 Property 对象中并获取解析的属性值(${x}
在 ant 属性中被替换)?例如使用这个属性文件:
foo=1
bar=${foo}.0
我需要获得bar
财产。有一个简单的方法吗?1.0
${foo}.0
编辑:亚历克斯的解决方案适用于简单的场景。在我的情况下,我必须解决导致这个问题的另一个问题:Pulling values from a Java Properties file in order? .
生成的加载和解析属性的示例代码:
import java.util.*;
import java.io.*;
import org.apache.commons.text.StringSubstitutor;
public class Prop {
Properties parsedProperties = null;
public static Properties parseProperties(String filename) {
// inner helper class keeping order of properties:
class LinkedProperties extends Properties {
private static final long serialVersionUID = 1L;
private final HashSet<Object> keys = new LinkedHashSet<Object>();
public LinkedProperties() {
}
public Iterable<Object> orderedKeys() {
return Collections.list(keys());
}
public Enumeration<Object> keys() {
return Collections.<Object>enumeration(keys);
}
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
}
LinkedProperties result = new LinkedProperties();
try (InputStream input = new FileInputStream(filename)) {
result.load(input);
@SuppressWarnings("unchecked")
StringSubstitutor sub = new StringSubstitutor((Map) result);
for (Object k : result.orderedKeys()) {
result.setProperty((String)k, sub.replace(result.getProperty((String)k)));
}
} catch (IOException ex) { ex.printStackTrace(); }
return ((Properties)result);
}
public static void main(String[] args) {
Prop app = new Prop();
// test - write sample properties file:
try {
PrintWriter writer = new PrintWriter(new FileWriter("config.properties"));
writer.println("foo=1");
writer.println("bar=1.${foo}");
writer.println("baz=${bar}.0");
writer.println("xxx=V.${baz}");
writer.close();
} catch (IOException ex) { ex.printStackTrace(); }
// read and parse properties:
app.parsedProperties = parseProperties("config.properties");
// debug print:
for (Object k : app.parsedProperties.keySet()) {
System.out.println((String)k + " = " + app.parsedProperties.getProperty((String)k));
}
}
}
解决方案
您可以StringSubstitutor
从 Apache Commons Text 使用,它的 Maven 依赖性非常适中(~200K):
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.8</version>
</dependency>
代码示例:
// init sample properties
Properties p = new Properties();
p.setProperty("foo", "${baz}.${baz}");
p.setProperty("bar", "${foo}.0");
p.setProperty("baz", "5");
Properties resolved = parseProperties(p);
System.out.println("resolved: " + resolved);
/////
public static Properties parseProperties(Properties orig) {
Properties result = new Properties();
StringSubstitutor sub = new StringSubstitutor((Map) orig);
orig.entrySet().forEach(e -> result.put(e.getKey(), sub.replace(e.getValue())));
return result;
}
输出:
resolved: {bar=5.5.0, foo=5.5, baz=5}
推荐阅读
- apache-beam - ProfileOptions profile_cpu=True 的数据流作业不写入配置文件
- firebase - 如何在 Android Studio 上使用 Firebase 重新验证 Facebook 登录用户?
- swift - SwiftUI:图像异步更改后视图不更新
- javascript - 反应对象数组的 setState 语法
- azure-maps - 使用 ng-azure-maps 将弹出窗口添加到 azure 地图图层
- flutter - Flutter、Consumer 中的 initState() 和 Provider 出错
(脏,依赖) - python - Python请求从url下载不起作用
- angular - 如何重构角度服务中的某些方法?
- javascript - 如何判断 javascript 字符串是数字还是日期?
- node.js - 如何在 NodeJS 中将 image.png 转换为二进制文件?