Java使用commons-configuration讀取配置檔案
pom.xml
<dependencies>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
</dependencies>
test.ini
; default config
[DEFAULT]
admin_http_listen = 127.0.0.1:80
admin_https_listen = 127.0.0.1:443
test.properties
A = one
B = two
C = three,four
D = True
讀取properties配置檔案
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import java.util.List;
/**
* @author xxx
*/
public class PropertiesFile {
/**
* 配置檔案存放路徑及檔名稱
*/
private String propertiesFilePath;
private PropertiesConfiguration cfg = null;
public PropertiesFile(String propertiesFilePath) {
try {
this.propertiesFilePath = propertiesFilePath;
cfg = new PropertiesConfiguration(propertiesFilePath);
} catch (ConfigurationException e) {
e.printStackTrace();
}
// 當檔案的內容發生改變時,配置物件也會重新整理
cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
}
public void setPropertiesFilePath(String propertiesFilePath) {
this.propertiesFilePath = propertiesFilePath;
}
/**
* 讀取properties檔案返回一個String型別的結果,如果是一組資料,例A = 1,2使用此方法get後將之會返回1
* @param key 需要查詢的key
* @return String型別的查詢結果
*/
public String getStringValue(String key) {
return cfg.getString(key);
}
/**
* 讀取properties檔案返回一個Int型別的結果,如果是一組資料,例A = 1,2使用此方法get後將之會返回1
* @param key 需要查詢的key
* @return Int型別的查詢結果
*/
public int getIntValue(String key) {
return cfg.getInt(key);
}
/**
* 讀取properties檔案返回一個Boolean型別的結果
* @param key 需要查詢的key
* @return Boolean型別的查詢結果
*/
public boolean getBooleanValue(String key) {
return cfg.getBoolean(key);
}
/**
* 讀取properties檔案返回一個結果List
* @param key 需要查詢的key
* @return List結果
*/
public List<?> getListValue(String key) {
return cfg.getList(key);
}
/**
* 讀取properties檔案返回一個結果陣列
* @param key 需要查詢的key
* @return 陣列結果
*/
public String[] getArrayValue(String key) {
return cfg.getStringArray(key);
}
public static void main(String[] args) {
PropertiesFile propertiesFile = new PropertiesFile("config/test.properties");
System.out.println(propertiesFile.getListValue("C"));
}
}
讀取ini檔案
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import java.util.List;
/**
* @author xxx
*/
public class INIFile {
/**
* 配置檔案存放路徑及檔名稱
*/
private String iniFilePath;
private HierarchicalINIConfiguration cfg = null;
public INIFile(String iniFilePath) {
try {
this.iniFilePath = iniFilePath;
cfg = new HierarchicalINIConfiguration(iniFilePath);
} catch (ConfigurationException e) {
e.printStackTrace();
}
// 當檔案的內容發生改變時,配置物件也會重新整理
cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
}
public void setIniFilePath(String iniFilePath) {
this.iniFilePath = iniFilePath;
}
/**
* 讀取ini檔案返回一個String型別的結果,如果是一組資料,例A = 1,2使用此方法get後將之會返回1
* @param key 需要查詢的key
* @return String型別的查詢結果
*/
public String getStringValue(String key) {
return cfg.getString(key);
}
/**
* 讀取ini檔案返回一個Int型別的結果,如果是一組資料,例A = 1,2使用此方法get後將之會返回1
* @param key 需要查詢的key
* @return Int型別的查詢結果
*/
public int getIntValue(String key) {
return cfg.getInt(key);
}
/**
* 讀取ini檔案返回一個Boolean型別的結果
* @param key 需要查詢的key
* @return Boolean型別的查詢結果
*/
public boolean getBooleanValue(String key) {
return cfg.getBoolean(key);
}
/**
* 讀取ini檔案返回一個結果List
* @param key 需要查詢的key
* @return List結果
*/
public List<?> getListValue(String key) {
return cfg.getList(key);
}
/**
* 讀取ini檔案返回一個結果陣列
* @param key 需要查詢的key
* @return 陣列結果
*/
public String[] getArrayValue(String key) {
return cfg.getStringArray(key);
}
public static void main(String[] args) {
INIFile iniFile = new INIFile("config/test.ini");
System.out.println(iniFile.getStringValue("DEFAULT.admin_http_listen"));
}
}
相關文章
- java中讀取配置檔案Java
- java中讀取.properties配置檔案Java
- Java讀取properties配置檔案工具包Java
- Java 讀取檔案Java
- viper 讀取配置檔案
- go配置檔案讀取Go
- Java動態指令碼Groovy讀取配置檔案Java指令碼
- JAVA 讀取xml檔案JavaXML
- java讀取properties檔案Java
- Golang專案中讀取配置檔案Golang
- Java系列:讀取XML檔案JavaXML
- Java高效讀取大檔案Java
- shell讀取配置檔案-sed命令
- springboot讀取配置檔案Spring Boot
- C#讀取ini配置檔案C#
- java 讀寫 ini 配置檔案Java
- Java屬性檔案的讀取Java
- Android讀取配置檔案的方法Android
- .net core 靈活讀取配置檔案
- python怎麼讀取配置檔案Python
- .NET Core 6.0之讀取配置檔案
- 索引器的妙用,讀取配置檔案索引
- 使用yaml檔案讀取資料YAML
- 配置檔案讀取——MySQL 多個連線MySql
- 透過python讀取ini配置檔案Python
- 如何在python中讀取配置檔案Python
- 文摘:在EJB中讀取XML配置檔案XML
- python讀取yaml配置檔案的方法PythonYAML
- Java實時讀取日誌檔案Java
- java讀取大檔案並處理Java
- Java 讀取txt檔案生成Word文件Java
- Java讀取Json檔案工具類JavaJSON
- Java讀取多層級xml檔案JavaXML
- Java API 讀取HDFS的單檔案JavaAPI
- java讀取resouces目錄下檔案Java
- java讀取大檔案1G+Java
- java mysql儲存和讀取檔案JavaMySql
- docker 啟動 jenkins,配置 mvn 卻無法使用 shell 讀取配置檔案DockerJenkins