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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM