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

    }

  • 相关阅读:
    Kaggle 神器 xgboost
    改善代码可测性的若干技巧
    IDEA 代码生成插件 CodeMaker
    Elasticsearch 使用中文分词
    Java性能调优的11个实用技巧
    Lucene 快速入门
    Java中一个字符用unicode编码为什么不是两字节
    lucene 的评分机制
    面向对象设计的 10 条戒律
    2019.10.23-最长全1串(双指针)
  • 原文地址:https://www.cnblogs.com/incognitor/p/8124666.html
Copyright © 2011-2022 走看看