首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

如何遍历properties文件的键和值

2012-01-19 
怎么遍历properties文件的键和值?例如有一个properties文件。里面有很多键 值对。有什么方法可以把propertie

怎么遍历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());        }    }} 

热点排行