dove19900520 发表于 2013-2-3 13:50:02

java中遍历properties属性文件的方法

以前一直在想如何遍历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;
 }
有什么不足的地方,请多多指教,共同进步,呵呵。
页: [1]
查看完整版本: java中遍历properties属性文件的方法