Java Properties配置文件和XML配置文件读取

一、properties类读取配置文件

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

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

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

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

11 InputStream in = new BufferedInputStream(new FileInputStream("D:/b.properties")); 22 properties.load(in); 33 Iterator<String> it = properties.stringPropertyNames().iterator(); 44 while (it.hasNext()) { 55 String key = it.next(); 66 System.out.println(key + ":" + properties.getProperty(key)); 77 }

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

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

11 FileInputStream in=new FileInputStream("D:/config.properties"); 22 properties.load(in);

  或

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

  或

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

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

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

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

11 ResourceBundle resource = ResourceBundle.getBundle("Config/config"); 22 String key = resource.getString("foo"); 33 System.out.println(key);

 三、XML配置文件读取

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

1、xml文件内容

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

2、XML项目中所在位置

3、测试代码

1 1 import org.dom4j.Document; 2 2 import org.dom4j.Element; 3 3 import org.dom4j.io.SAXReader; 4 4 import java.io.File; 5 5 import java.net.URL; 6 6 import java.util.Iterator; 7 7 8 8 try { 9 9 URL filePath=null; 1010 filePath = HelloWorld.class.getClassLoader().getResource("config/aaa.xml"); 1111 File f =new File(filePath.getPath()); 1212 if (!f.exists()) { 1313 System.out.println(" Error : Config file doesn't exist!"); 1414 System.exit(1); 1515 } 1616 SAXReader reader = new SAXReader(); 1717 Document doc; 1818 doc = reader.read(f); 1919 Element root = doc.getRootElement(); 2020 Element data; 2121 Iterator<?> itr = root.elementIterator("VALUE"); 2222 data = (Element) itr.next(); 2323 String server = data.elementText("server").trim(); 2424 String user = data.elementText("user").trim(); 2525 String pass = data.elementText("pass").trim(); 2626 String port = data.elementText("port").trim(); 2727 String dbname = data.elementText("dbname").trim(); 2828 System.out.println(server); 2929 System.out.println(user); 3030 System.out.println(pass); 3131 System.out.println(port); 3232 System.out.println(dbname); 3333 } catch (Exception ex) { 3434 System.out.println("Error : " + ex.toString()); 3535 }

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )