【发布时间】:2012-03-29 03:54:36
【问题描述】:
我正在从数据库中读取一个属性文件。
我检查了java.util.Properties 并且没有从String 实例解析的方法。有什么办法吗?
【问题讨论】:
-
属性文件的格式是什么?
-
来自数据库的属性文件或属性??
标签: java
我正在从数据库中读取一个属性文件。
我检查了java.util.Properties 并且没有从String 实例解析的方法。有什么办法吗?
【问题讨论】:
标签: java
你说得对,java.util.Properties 没有从 String 读取的方法 - 但实际上它具有从 InputStream 或 Reader 读取的更通用的方法。
因此,如果您有某种方式可以将您的 String 呈现为其中任何一种,您可以调用 load,即有效地逐个迭代字符的源。感觉它应该存在,而且确实存在 - java.io.StringReader。
那么,将它们放在一起非常简单:
public Properties parsePropertiesString(String s) {
// grr at load() returning void rather than the Properties object
// so this takes 3 lines instead of "return new Properties().load(...);"
final Properties p = new Properties();
p.load(new StringReader(s));
return p;
}
【讨论】:
这两个实用方法可以通过java.util.Properties 对象写入和读取String:
/**
* Converts a {@link Properties} object to {@link String} and you can
* also provide a description for the output.
*
* @param props an input {@link Properties} to be converted to {@link String}
* @param desc an input description for the output
* @return an output String that could easily parse back to {@link Properties} object
* @throws IOException If writing encounter a problem or if an I/O error occurs.
*/
private static String convert2String(final Properties props, String desc) throws IOException {
final StringWriter sw = new StringWriter();
String propStr;
try {
props.store(sw, desc);
propStr = sw.toString();
} finally {
if (sw != null) {
sw.close();
}
}
return propStr;
}
/**
* Converts {@link String} to {@link Properties}
* @param propsStr an {@link String} input that is saved via convert2String method
* @return a {@link Properties} object
* @throws IOException if an error occurred when reading from the {@link StringReader}
*/
private static Properties convert2Properties(final String propsStr) throws IOException {
final Properties props = new Properties();
final StringReader sr = new StringReader(propsStr);
try {
props.load(sr);
} finally {
if (sr != null)
sr.close();
}
return props;
}
我发现上述两种方法在有大量属性时很有用 并且您想将它们全部保存在
storage or database中,并且您不想创建一个巨大的key-value存储表。
【讨论】:
我使用此代码从单个 DB 列加载属性
public Properties buildProperties(String propertiesFromString, String entrySeparator) throws IOException {
Properties properties = new Properties();
properties.load(new StringReader(propertiesFromString.replaceAll(entrySeparator, "\n")));
return properties;
}
通过一个简单的测试
@Test
public void testProperties() throws Exception {
Properties properties = buildProperties("A=1;B=2;Z=x",";");
assertEquals("1", properties.getProperty("A"));
assertEquals("2", properties.getProperty("B"));
assertEquals("3", properties.getProperty("C","3"));
assertNull(properties.getProperty("Y"));
assertEquals("x", properties.getProperty("Z"));
}
【讨论】:
我们遇到了类似的问题,以上方法对我们不起作用。
然而,下面的却做到了。
def content = readFile 'gradle.properties'
Properties properties = new Properties()
InputStream is = new ByteArrayInputStream(content.getBytes());
properties.load(is)
def runtimeString = 'SERVICE_VERSION_MINOR'
echo properties."$runtimeString"
SERVICE_VERSION_MINOR = properties."$runtimeString"
echo SERVICE_VERSION_MINOR
【讨论】: