【问题标题】:Strange display using java.util.Properties [closed]使用 java.util.Properties 的奇怪显示 [关闭]
【发布时间】:2011-07-28 13:36:50
【问题描述】:

我有一个 .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++;
}

你认为我会有什么输出?

我会在你回答后编辑问题。

谢谢。

【问题讨论】:

    标签: java properties iterator fileinputstream


    【解决方案1】:

    Properties 对象由Map 实现支持,因此不要依赖属性的顺序。如果您还有其他要报告为“奇怪”的内容,请详细说明您的问题。 :-)

    【讨论】:

      【解决方案2】:

      行号不相关,因为Properties 使用散列来存储元素。订单未保留。

      【讨论】:

        猜你喜欢
        • 2012-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        相关资源
        最近更新 更多