zoukankan      html  css  js  c++  java
  • Java 读取配置文件

    1.读取XML文件使用dom4j-full.jar包的SAXReader解析;

    Document document=new SAXReader.reader(“xml文路径/文件名xxx.xml”);//得到Document对象

    Element root = document.getRootElement()//获得根节点

    Iterator iterator=root.elementIterator(); //从根节点遍历子节点

    Iterator iterator=Element.elementIterator(); //再从子节点在遍历其子节点

    对节点访问其属性用:Attribute leaderAttr =Element. attribute(“xxx”);
    对节点访问其某个属性leaderAttr的名称:leaderAttr.getName();
    对节点访问其某个属性leaderAttr的值:leaderAttr.getValue()
    对节点访问其名称:Element.getName();
    对节点访问其文本:Element. getText();

    注明:一般使用SAXReader.reader(InputStreamReader); Class有个静态方法getResourceAsStream(classPathUrl)返回InputStream;

    2.读取properties文件,用到java.util包下的PropertyResourceBundle、ResourceBundle

    PropertyResourceBundle configBundle = (PropertyResourceBundle) ResourceBundle.getBundle(fileClassPathUrl);//fileClassPathUrl不用带后缀.properties

    String result = = configBundle.getString(key); //读取key的值

    //读取全部的key值

    Enumeration<String> emun = configBundle.getKeys();

    while (emun.hasMoreElements()) {

      String key = emun.nextElement();

      map.put(key, configBundle.getString(key));

    }

  • 相关阅读:
    面向对象基础
    JS操作属性和样式
    表单验证
    form标签
    Dreamweaver网页设计代码大全
    最差项目展示
    CSS样式表
    while循环 do while循环 switch
    for循环
    穷举法
  • 原文地址:https://www.cnblogs.com/incognitor/p/8124666.html
Copyright © 2011-2022 走看看