日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/8 编程问答 37 如意码农
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Java Properties配置文件和XML配置文件读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。