以前一直在想如何遍历properties属性文件,但一直没有实现过,今天,由于编程需要,通过查资料实现了该功能,现将代码粘贴上,给大家共享一下:
//直接遍历
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());
}
}
}
///
/**
* 将properties属性文件转换成list类型数据
*
* @param fileName
* properties属性文件名
* @return List集合
*/
public static List<Entry<Object, Object>> propToList(String fileName) {
Properties props = readPorp(fileName);
Iterator<Entry<Object, Object>> it = props.entrySet().iterator();
List<Entry<Object, Object>> list = new ArrayList<Entry<Object, Object>>();
while (it.hasNext()) {
Entry<Object, Object> entry = (Entry<Object, Object>) it.next();
list.add(entry);
// logger.info(entry.getKey()+" : "+entry.getValue());
}
return list;
}