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));

    }

  • 相关阅读:
    Vue
    Vue
    Vue
    服务器上部署django项目流程?
    Git 命令
    git命令?
    消息队列中间件??
    简述COOKIE和SESSION的区别与联系?
    什么是restful API?
    Django、Flask、Tornado的区别?
  • 原文地址:https://www.cnblogs.com/incognitor/p/8124666.html
Copyright © 2011-2022 走看看