properties,xml,yaml配置檔案解析
讀取幾種配置檔案
java中,常見幾種配置檔案有:.properties
,.xml
,.yml
,spring專案中對這些型別的配置檔案通常有現成可用的封裝,下面用手動解析的方式來操作這些型別配置檔案;
首先idea建立專案:
- properties
- xml
- yaml
- 總結
根目錄下建立配置檔案:application.properties
,和測試類Test.java
application.properties
配置檔案中,設定幾個基礎配置項:
Test.java
中編寫readFromProperties()
方法解析檔案:
public static Map readFromProperties() {
Map result = new HashMap<>();
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream("read-config/application.properties")) {
properties.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
if (!properties.keySet().isEmpty()) {
for (Object key : properties.keySet()) {
result.put(key.toString(), properties.getProperty(key.toString()));
}
}
return result;
}
.properties
配置檔案可以通過jdk中的java.util.Properties
類進行讀取和解析,注意application.properties
檔案路徑。測試:
在application.properties
同級目錄下建立xml配置檔案config.xml
,並寫入簡單配置資訊:
Test.java
中編寫readFromXml()
方法解析檔案:
public static Map readFromXml() {
Map result = new HashMap<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("read-config/config.xml"));
result = node(document);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static Map node(Node node) {
Map result = new HashMap<>();
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
//元素節點-遞迴
if (item.getNodeType() == Node.ELEMENT_NODE) {
result.putAll(node(item));
}
//文字節點-新增map
else if (item.getNodeType() == Node.TEXT_NODE
&& null != item.getNodeValue()
&& !"".equals(item.getNodeValue().trim())
&& !"\n".equals(item.getNodeValue().trim())) {
result.put(item.getParentNode().getNodeName(), item.getNodeValue());
}
}
return result;
}
程式碼分析:
xml解析主要用到javax.xml.parsers
包下的兩個類:DocumentBuilderFactory
和DocumentBuilder
,通過documentBuilder.parse(new File("read-config/config.xml"))
方法解析xml檔案為Document物件。接著用方法node(Node node)
遞迴Document物件各節點到Map物件。Document本身也是Node物件
在application.properties
同級目錄下建立yaml配置檔案config.yml
,並寫入簡單配置資訊:
由於jdk沒有提供yaml解析工具,需要手動匯入解析庫,這裡用的庫是:snakeyaml-1.29.jar
通過snakeyaml庫,可以解析yaml配置檔案為map或者物件。
該工具庫下載地址參考:https://mvnrepository.com/artifact/org.yaml/snakeyaml
解析到Map
public static void readFromYml2Map() {
try (FileReader reader = new FileReader("read-config/config.yml")) {
Yaml yaml = new Yaml();
Object load = yaml.load(reader);
if (load instanceof Map) {
Map
程式碼說明:解析到Map核心方法是通過yaml.load(reader)
,該方法將會返回一個Map物件。輸出結果如下:
解析為物件
public static void readFromYml2Entity() {
try (FileReader reader = new FileReader("read-config/config.yml")) {
Yaml yaml = new Yaml();
ConfigEntity entity = yaml.loadAs(reader, ConfigEntity.class);
System.out.println("======");
System.out.println(entity);
System.out.println("======");
} catch (Exception e) {
e.printStackTrace();
}
}
程式碼說明:解析到物件核心方法是通過yaml.loadAs(reader, ConfigEntity.class)
。輸出結果如下:以上就是properties,xml,yaml三種配置檔案解析的方法,這三種配置檔案個人建議是簡單的配置用properties,層次結構複雜的配置建議用yaml,xml雖然結構清晰但是配置沒yaml簡潔可根據情況取捨。
尊重原創,轉載請標明出處,謝謝