Java Properties 类实现处理双引号/单引号值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14716281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Properties class implementation handles double/single quoted values?
提问by Eric
I want to support property file format like below (allow quotes surround the value):
我想支持如下所示的属性文件格式(允许用引号将值括起来):
key1=value1
key2="value2"
key3='value'
My question is does Java Properties class implementation handles double/single quoted values like above? I mean auto-removing quotes.
我的问题是 Java Properties 类实现是否像上面那样处理双引号/单引号值?我的意思是自动删除引号。
Actually I tried it's not, just want to confirm here. So I have to remove quotes myself.
其实我试过不是,只是想在这里确认一下。所以我必须自己删除引号。
EDIT:
编辑:
I had a code below for my simple case:
对于我的简单案例,我在下面有一个代码:
String path = "/tmp/my.properties";
Properties p = new Properties();
p.load(new FileInputStream(new File(path)));
String v = p.getProperty("key2");
if((v.startsWith("\"") && v.endsWith("\"")) ||
(v.startsWith("\'") && v.endsWith("\'"))) {
v = v.substring(1, v.length()-1);
}
Any recommendation on best practice to handle this?
有关处理此问题的最佳实践的任何建议?
Thanks
谢谢
回答by Dino Octavian
To remove quotes, load the property file with your own extension of ResourceBundle
which overrides handleGetObject
.
要删除引号,请使用您自己的扩展名加载属性文件,ResourceBundle
其覆盖handleGetObject
.
See also:
也可以看看:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.htmlhttp://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html