日期:2014-05-20  浏览次数:20966 次

怎么遍历properties文件的键和值?
例如有一个properties文件。里面有很多键 值对。有什么方法可以把properties文件里面的键值队一次全打印出来。或保存在数组里面
而不使用getproperty一个一个的取。最好给个例子。谢谢了!!!

------解决方案--------------------
import java.util.Enumeration;
import java.util.Properties;

public class Test {
public static void main(String arg[]) {
Properties prop = new Properties();
prop.put(1, "a");
prop.put(2, "b");
Enumeration enums = prop.keys();
while (enums.hasMoreElements()) {
Object key = enums.nextElement();
System.out.println("key = " + key + " value = " + prop.get(key));
}
}
}

------解决方案--------------------
给你一sample, 在c盘更目录放一个p.properties文件就可以测试了。
Java code

public class TestProperties {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(new File("c:\\p.properties")));
        Iterator itr = p.entrySet().iterator();
        while (itr.hasNext()){
            Entry e = (Entry)itr.next();
            System.out.println(e.getKey() + ": " + e.getValue());
        }
    }

}