六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 55|回复: 0

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

[复制链接]

升级  12%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
68
 楼主| 发表于 2013-2-3 13:50:02 | 显示全部楼层 |阅读模式
以前一直在想如何遍历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;
 }
有什么不足的地方,请多多指教,共同进步,呵呵。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表