read/write 属性文件如何用冒号“:”而不是等于“=”分隔键和值
how read/write properties file which seperated key and value with colon ':' instead of equal '='
我有包含 key:value 列表的属性文件,如下所示:
key1 : value1
key2 : value2
如何使用属性 class 读写此文件?可不可以?
是的。你考虑过尝试一下吗?或咨询 documentation?
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
硬核解决方案。
将此文件作为文本文件读入字符串。 See example.
将:
替换为=
。
从字符串解析属性,参见Parsing string as properties
public Properties parsePropertiesString(String s) {
final Properties p = new Properties();
p.load(new StringReader(s));
return p;
}
如果要将属性写入文件,请以相反的顺序执行相同的操作。将属性写入字符串,将=
替换为:
,并将字符串写入文件。
您可以使用以下代码读取属性文件。请注意“:”与“=”的作用相同。
try (InputStream in = new FileInputStream("filename.propeties")) {
Properties prop = new Properties();
prop.load(in);
for (String key: prop.stringPropertyNames()) {
String value = prop.getProperty(key);
System.out.println(key+ "=" + value);
}
} catch (IOException e) {
e.printStackTrace();
}
我查看 java 属性 class(在 jdk 中),发现 java 使用常量 '=' 通过以下方法保存属性文件:
private void store0(BufferedWriter bw, String comments, boolean escUnicode)
throws IOException
{
if (comments != null) {
writeComments(bw, comments);
}
bw.write("#" + new Date().toString());
bw.newLine();
synchronized (this) {
for (Enumeration<?> e = keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
String val = (String)get(key);
key = saveConvert(key, true, escUnicode);
/* No need to escape embedded and trailing spaces for value, hence
* pass false to flag.
*/
val = saveConvert(val, false, escUnicode);
bw.write(key + "=" + val);
bw.newLine();
}
}
bw.flush();
}
所以,无法处理我的属性文件。但是对于 erad/write 我的属性文件,我发现这个文件是一个 yaml 格式,我们可以通过这种方法轻松读取文件:
public Map read(String path) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
File file = new File(path);
Map map = mapper.readValue(file, Map.class);
return map;
} catch (Exception e) {
logger.error("Error during read Yml file {}", path, e);
}
return new HashMap();
}
并通过以下方法更新文件:
public boolean update(String path, Map content) {
try {
YAMLFactory yamlFactory = new YAMLFactory();
yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
File file = new File(path);
ObjectMapper mapper = new ObjectMapper(yamlFactory);
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValue(file, content);
return true;
} catch (Exception e) {
logger.error("Error during save Yml file {}", path, e);
}
return false;
}
重要的是我使用以下代码来防止对值使用双引号:
yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
我有包含 key:value 列表的属性文件,如下所示:
key1 : value1
key2 : value2
如何使用属性 class 读写此文件?可不可以?
是的。你考虑过尝试一下吗?或咨询 documentation?
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
硬核解决方案。
将此文件作为文本文件读入字符串。 See example.
将
:
替换为=
。从字符串解析属性,参见Parsing string as properties
public Properties parsePropertiesString(String s) { final Properties p = new Properties(); p.load(new StringReader(s)); return p; }
如果要将属性写入文件,请以相反的顺序执行相同的操作。将属性写入字符串,将=
替换为:
,并将字符串写入文件。
您可以使用以下代码读取属性文件。请注意“:”与“=”的作用相同。
try (InputStream in = new FileInputStream("filename.propeties")) {
Properties prop = new Properties();
prop.load(in);
for (String key: prop.stringPropertyNames()) {
String value = prop.getProperty(key);
System.out.println(key+ "=" + value);
}
} catch (IOException e) {
e.printStackTrace();
}
我查看 java 属性 class(在 jdk 中),发现 java 使用常量 '=' 通过以下方法保存属性文件:
private void store0(BufferedWriter bw, String comments, boolean escUnicode)
throws IOException
{
if (comments != null) {
writeComments(bw, comments);
}
bw.write("#" + new Date().toString());
bw.newLine();
synchronized (this) {
for (Enumeration<?> e = keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
String val = (String)get(key);
key = saveConvert(key, true, escUnicode);
/* No need to escape embedded and trailing spaces for value, hence
* pass false to flag.
*/
val = saveConvert(val, false, escUnicode);
bw.write(key + "=" + val);
bw.newLine();
}
}
bw.flush();
}
所以,无法处理我的属性文件。但是对于 erad/write 我的属性文件,我发现这个文件是一个 yaml 格式,我们可以通过这种方法轻松读取文件:
public Map read(String path) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
File file = new File(path);
Map map = mapper.readValue(file, Map.class);
return map;
} catch (Exception e) {
logger.error("Error during read Yml file {}", path, e);
}
return new HashMap();
}
并通过以下方法更新文件:
public boolean update(String path, Map content) {
try {
YAMLFactory yamlFactory = new YAMLFactory();
yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
File file = new File(path);
ObjectMapper mapper = new ObjectMapper(yamlFactory);
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValue(file, content);
return true;
} catch (Exception e) {
logger.error("Error during save Yml file {}", path, e);
}
return false;
}
重要的是我使用以下代码来防止对值使用双引号:
yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);