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

    一、properties类读取配置文件

    1、从项目中读取properties配置文件,新建一个main程序,对应位置建立一个名称为config.properties配置文件,随意放置一些键值对。IDEA建立配置文件,对应package鼠标右键,点击New,再点击Resource Bundle,弹出对话框输入名称保存即可。

     

    2、以下实例,读取配置文件,然后遍历key值和value值,采用此种方式读取项目中配置文件,将不依赖具体环境。

     1 public class HelloWorld {
     2     public static void main(String[] args) {
     3         Properties properties=new Properties();
     4         InputStream in=HelloWorld.class.getClassLoader().getResourceAsStream("Config/config.properties");
     5         try{
     6             properties.load(in);
     7             Enumeration enumeration=properties.propertyNames();
     8             while (enumeration.hasMoreElements()){
     9                 String strKey=(String)enumeration.nextElement();
    10                 String strValue=properties.getProperty(strKey);
    11                 System.out.println("key:"+strKey+";Value:"+strValue);
    12             }
    13         }catch(Exception e){
    14             System.out.println("There is An IO error!");
    15         }
    16     }
    17 }

      (1)读取后另一种遍历方式

    1     InputStream in = new BufferedInputStream(new FileInputStream("D:/b.properties"));
    2     properties.load(in);
    3     Iterator<String> it = properties.stringPropertyNames().iterator();
    4             while (it.hasNext()) {
    5                 String key = it.next();
    6                 System.out.println(key + ":" + properties.getProperty(key));
    7             }

    3、一些其它读取配置文件的方法

      (1)从环境固定位置读取配置文件,配置文件放在某个磁盘下几种方式。   

    1    FileInputStream in=new FileInputStream("D:/config.properties");
    2    properties.load(in);    

      或

    1    InputStream in= new BufferedInputStream(newFileInputStream("D:/config.properties"));
    2    properties.load(in);

      或

    1    BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/config.properties"));
    2    properties.load(bufferedReader);

    4、配置文件添加新的键值对,然后将保存成一个新文件并存储。

    1    FileOutputStream oFile = new FileOutputStream("D:/b.properties", true);//true表示追加打开
    2    properties.setProperty("移动", "10086");
    3    properties.store(oFile, "The New properties file has Created");
    4    oFile.close();

    二、java.util.ResourceBundle类读取properties配置文件,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

    1    ResourceBundle resource = ResourceBundle.getBundle("Config/config");
    2    String key = resource.getString("foo");
    3    System.out.println(key);

     三、XML配置文件读取

    使用dom4j解析xml,下载地址:https://dom4j.github.io/

    1、xml文件内容

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <CONFIG>
     3     <VALUE>
     4         <!-- mysql连接设置 -->
     5         <server>127.0.0.1</server>
     6         <dbname>users</dbname>
     7         <user>root</user>
     8         <pass>pass</pass>
     9         <port>3306</port>
    10     </VALUE>
    11 </CONFIG>

    2、XML项目中所在位置

    3、测试代码

     1 import org.dom4j.Document;
     2 import org.dom4j.Element;
     3 import org.dom4j.io.SAXReader;
     4 import java.io.File;
     5 import java.net.URL;
     6 import java.util.Iterator;
     7 
     8 try {
     9             URL filePath=null;
    10             filePath = HelloWorld.class.getClassLoader().getResource("config/aaa.xml");
    11             File f =new File(filePath.getPath());
    12             if (!f.exists()) {
    13                 System.out.println("  Error : Config file doesn't exist!");
    14                 System.exit(1);
    15             }
    16             SAXReader reader = new SAXReader();
    17             Document doc;
    18             doc = reader.read(f);
    19             Element root = doc.getRootElement();
    20             Element data;
    21             Iterator<?> itr = root.elementIterator("VALUE");
    22             data = (Element) itr.next();
    23             String server = data.elementText("server").trim();
    24             String user = data.elementText("user").trim();
    25             String pass = data.elementText("pass").trim();
    26             String port = data.elementText("port").trim();
    27             String dbname = data.elementText("dbname").trim();
    28             System.out.println(server);
    29             System.out.println(user);
    30             System.out.println(pass);
    31             System.out.println(port);
    32             System.out.println(dbname);
    33         } catch (Exception ex) {
    34             System.out.println("Error : " + ex.toString());
    35         }

    4、注意事项:以上的获取XML配置资源,主要依赖第三方包dom4j,下载地址在上面已经给出。将第三方jar包引用到当前项目可以参考:https://blog.csdn.net/MAOZEXIJR/article/details/88966876

    四、尊重原创,以上有些内容参考了以下链接内容。

               https://www.cnblogs.com/mengxiangqihang/p/4150450.html

               https://www.cnblogs.com/xudong-bupt/p/3758136.html

               https://www.cnblogs.com/bakari/p/3562244.html

          https://www.cnblogs.com/xudong-bupt/p/3758136.html

  • 相关阅读:
    电脑无损换硬盘,不用重装系统驱动的小技巧
    OSPF协议原理及配置5-LSA分析
    OSPF协议原理及配置3-邻居关系的建立
    OSPF协议原理及配置2-理解邻居和邻接关系
    我在华为写代码
    嵌入式未来发展
    blog to live,do not love to blog
    浮点数转换为新类型时必须做范围检查
    分享点干货
    基础C语言知识串串香14☞增补知识
  • 原文地址:https://www.cnblogs.com/mojiejushi/p/12594940.html
Copyright © 2011-2022 走看看