try{
FileInputStream in = new FileInputStream("1.text");
Properties pro = new Properties();
pro.load(in);
Enumeration enum = pro.propertyNames();
while(enum.hasMoreElements())
{
    String key = (String)enum.nextElement();
                    String value_url = pro.getProperty(key);
    System.out.println("first"+key+"="+value_url);
}
in.close();
             }
     catch(Exception e)
     {
        e.printStackTrace();
     }
这个怎么只输出1.text文件中的最后一行,好像也只得到最后一行
1.text内容:
#Fri Mar 28 18:14:28 CST 2008
25=http\://127.0.0.1\:8080/wal/3.jpg
#Fri Mar 28 18:14:28 CST 2008
25=http\://127.0.0.1\:8080/wal/2.jpg
#Fri Mar 28 18:14:28 CST 2008
25=http\://127.0.0.1\:8080/wal/1.jpg
这里面的也是读进去的。
请问各位高手怎么回事呀?

解决方案 »

  1.   

    得到的key都是25?做个测试,改成下面这样看是否可以得到正确结果.
    #Fri Mar 28 18:14:28 CST 2008 
    25=http\://127.0.0.1\:8080/wal/3.jpg 
    #Fri Mar 28 18:14:28 CST 2008 
    26=http\://127.0.0.1\:8080/wal/2.jpg 
    #Fri Mar 28 18:14:28 CST 2008 
    27=http\://127.0.0.1\:8080/wal/1.jpg 
      

  2.   

    干嘛用重复的键
    Properties内部其实用的也是HashMap
    可以看jdk源码
      

  3.   

    后面的元素覆盖了前面的
    Enumeration 只剩下一个元素楼主应该知道map是如何操作
      

  4.   

    恩,果然是奇迹,只可惜无法证实循环一次是正常的,因为enum只剩下一个元素,前两个被覆盖了
      

  5.   

    将文本文件修改如下:
    #Fri Mar 28 18:14:28 CST 2008
    25=http\://127.0.0.1\:8080/wal/3.jpg
    #Fri Mar 28 18:14:28 CST 2008
    26=http\://127.0.0.1\:8080/wal/2.jpg
    #Fri Mar 28 18:14:28 CST 2008
    27=http\://127.0.0.1\:8080/wal/1.jpg
    经测试是可以的
    附测试代码: public static void main(String[] args) throws FileNotFoundException, IOException{
            Reader reader = null;
            try {
                reader = new FileReader("a.txt");            Properties properties = new Properties();
                properties.load(reader);
                
                Enumeration enumeration = properties.propertyNames();
                while(enumeration.hasMoreElements()){
                    String key = enumeration.nextElement().toString();
                    System.out.println("key:"+key);
                    
                    String value = properties.getProperty(key);
                    System.out.println("value:"+value);
                }
            } finally {
                reader.close();
            }
        }
      

  6.   

    上述程序测试结果如下:
    key:27
    value:http://127.0.0.1:8080/wal/1.jpg
    key:26
    value:http://127.0.0.1:8080/wal/2.jpg
    key:25
    value:http://127.0.0.1:8080/wal/3.jpg