使用 java.util.Properties 的奇怪显示
我有一个 .properties 文件,其格式如下:
toto=titi
fofo=fifi
coco=cici
mama=momo
dada=didi
解析此文件时,我的显示很奇怪。这是我正在使用的代码:
Properties prop = new Properties();
String fileLocation = "C:/myProperties.properties";
prop.load(new FileInputStream(fileLocation));
Iterator<Object> it = prop.keySet().iterator();
int line = 0;
while (it.hasNext())
{
String propertyName = (String) it.next();
if (propertyName.equals("coco"))
{
System.out.println("coco found at line : " + line);
break;
}
else if (propertyName.equals("titi"))
{
System.out.println("Titi found at line : " + line);
break;
}
line++;
}
您认为我会输出什么?
我将在您回答后编辑问题。
谢谢。
I have a .properties file, who has this format :
toto=titi
fofo=fifi
coco=cici
mama=momo
dada=didi
I'm having a strange display when I parse this file. This is the code I'm using :
Properties prop = new Properties();
String fileLocation = "C:/myProperties.properties";
prop.load(new FileInputStream(fileLocation));
Iterator<Object> it = prop.keySet().iterator();
int line = 0;
while (it.hasNext())
{
String propertyName = (String) it.next();
if (propertyName.equals("coco"))
{
System.out.println("coco found at line : " + line);
break;
}
else if (propertyName.equals("titi"))
{
System.out.println("Titi found at line : " + line);
break;
}
line++;
}
What do you think I will have in output ?
I will edit the question after your answers.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Properties
对象由Map
实现支持,因此不要依赖属性的顺序。如果您还有其他要报告为“奇怪”的事情,请详细说明您的问题。 :-)Properties
object is backed by aMap
implementation so don't rely on the ordering of your properties. If you have something else to report as "strange" please elaborate your question. :-)行号不相关,因为
Properties
使用哈希来存储元素。订单不保留。Line number is not relevant as
Properties
uses hash to store elements. Order is not preserved.