Java讀取.properties配置文件的幾種方式
Java 開(kāi)發(fā)中,需要將一些易變的配置參數(shù)放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通過(guò) DOM 或 SAX 方式解析,而讀取 properties 配置文件就比較容易。
介紹幾種讀取方式:
1、基于ClassLoder讀取配置文件
注意:該方式只能讀取類(lèi)路徑下的配置文件,有局限但是如果配置文件在類(lèi)路徑下比較方便。
Properties properties = new Properties();
// 使用ClassLoader加載properties配置文件生成對(duì)應(yīng)的輸入流
InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");
// 使用properties對(duì)象加載輸入流
properties.load(in);
//獲取key對(duì)應(yīng)的value值
properties.getProperty(String key);
2、基于 InputStream 讀取配置文件
注意:該方式的優(yōu)點(diǎn)在于可以讀取任意路徑下的配置文件
Properties properties = new Properties();
// 使用InPutStream流讀取properties文件
BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/config.properties"));
properties.load(bufferedReader);
// 獲取key對(duì)應(yīng)的value值
properties.getProperty(String key);
3、通過(guò) java.util.ResourceBundle 類(lèi)來(lái)讀取,這種方式比使用 Properties 要方便一些
1>通過(guò) ResourceBundle.getBundle() 靜態(tài)方法來(lái)獲?。≧esourceBundle是一個(gè)抽象類(lèi)),這種方式來(lái)獲取properties屬性文件不需要加.properties后綴名,只需要文件名即可
properties.getProperty(String key);
//config為屬性文件名,放在包c(diǎn)om.test.config下,如果是放在src下,直接用config即可
ResourceBundle resource = ResourceBundle.getBundle("com/test/config/config");
String key = resource.getString("keyWord");
2>從 InputStream 中讀取,獲取 InputStream 的方法和上面一樣,不再贅述
ResourceBundle resource = new PropertyResourceBundle(inStream);
注意:在使用中遇到的最大的問(wèn)題可能是配置文件的路徑問(wèn)題,如果配置文件入在當(dāng)前類(lèi)所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,則要使用com/test/config/config.properties(通過(guò)Properties來(lái)獲?。┗騝om/test/config/config(通過(guò)ResourceBundle來(lái)獲?。?;屬性文件在src根目錄下,則直接使用config.properties或config即可。
下面附上幾種方式的測(cè)試代碼,僅供參考:
package com.test.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;
/**
*
* @ClassName: TestProperties
* @Description: 獲取配置文件信息
* @date: 2017年11月25日 上午10:56:00
* @version: 1.0.0
*/
public class TestProperties {
/**
*
* @Title: printAllProperty
* @Description: 輸出所有配置信息
* @param props
* @return void
* @throws
*/
private static void printAllProperty(Properties props)
{
@SuppressWarnings("rawtypes")
Enumeration en = props.propertyNames();
while (en.hasMoreElements())
{
String key = (String) en.nextElement();
String value = props.getProperty(key);
System.out.println(key + " : " + value);
}
}
/**
* 根據(jù)key讀取value
*
* @Title: getProperties_1
* @Description: 第一種方式:根據(jù)文件名使用spring中的工具類(lèi)進(jìn)行解析
* filePath是相對(duì)路勁,文件需在classpath目錄下
* 比如:config.properties在包c(diǎn)om.test.config下,
* 路徑就是com/test/config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_1(String filePath, String keyWord){
Properties prop = null;
String value = null;
try {
// 通過(guò)Spring中的PropertiesLoaderUtils工具類(lèi)進(jìn)行獲取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
// 根據(jù)關(guān)鍵字查詢(xún)相應(yīng)的值
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
/**
* 讀取配置文件所有信息
*
* @Title: getProperties_1
* @Description: 第一種方式:根據(jù)文件名使用Spring中的工具類(lèi)進(jìn)行解析
* filePath是相對(duì)路勁,文件需在classpath目錄下
* 比如:config.properties在包c(diǎn)om.test.config下,
* 路徑就是com/test/config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_1(String filePath){
Properties prop = null;
try {
// 通過(guò)Spring中的PropertiesLoaderUtils工具類(lèi)進(jìn)行獲取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根據(jù)key讀取value
*
* @Title: getProperties_2
* @Description: 第二種方式:使用緩沖輸入流讀取配置文件,然后將其加載,再按需操作
* 絕對(duì)路徑或相對(duì)路徑, 如果是相對(duì)路徑,則從當(dāng)前項(xiàng)目下的目錄開(kāi)始計(jì)算,
* 如:當(dāng)前項(xiàng)目路徑/config/config.properties,
* 相對(duì)路徑就是config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_2(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
// 通過(guò)輸入緩沖流進(jìn)行讀取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加載輸入流
prop.load(InputStream);
// 根據(jù)關(guān)鍵字獲取value值
value = prop.getProperty(keyWord);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 讀取配置文件所有信息
*
* @Title: getProperties_2
* @Description: 第二種方式:使用緩沖輸入流讀取配置文件,然后將其加載,再按需操作
* 絕對(duì)路徑或相對(duì)路徑, 如果是相對(duì)路徑,則從當(dāng)前項(xiàng)目下的目錄開(kāi)始計(jì)算,
* 如:當(dāng)前項(xiàng)目路徑/config/config.properties,
* 相對(duì)路徑就是config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_2(String filePath){
Properties prop = new Properties();
try {
// 通過(guò)輸入緩沖流進(jìn)行讀取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加載輸入流
prop.load(InputStream);
printAllProperty(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根據(jù)key讀取value
*
* @Title: getProperties_3
* @Description: 第三種方式:
* 相對(duì)路徑, properties文件需在classpath目錄下,
* 比如:config.properties在包c(diǎn)om.test.config下,
* 路徑就是/com/test/config/config.properties
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_3(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
/**
* 讀取配置文件所有信息
*
* @Title: getProperties_3
* @Description: 第三種方式:
* 相對(duì)路徑, properties文件需在classpath目錄下,
* 比如:config.properties在包c(diǎn)om.test.config下,
* 路徑就是/com/test/config/config.properties
* @param filePath
* @return
* @throws
*/
public static void getProperties_3(String filePath){
Properties prop = new Properties();
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 注意路徑問(wèn)題
String properties_1 = getProperties_1("com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_1);
getProperties_1("com/test/config/config.properties");
System.out.println("*********************************************");
// 注意路徑問(wèn)題
String properties_2 = getProperties_2("configure/configure.properties", "jdbc.url");
System.out.println("jdbc.url = " + properties_2);
getProperties_2("configure/configure.properties");
System.out.println("*********************************************");
// 注意路徑問(wèn)題
String properties_3 = getProperties_3("/com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_3);
getProperties_3("/com/test/config/config.properties");
}
}
到此這篇關(guān)于Java讀取.properties配置文件的幾種方式的文章就介紹到這了,更多相關(guān)Java 讀取properties 配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java語(yǔ)法基礎(chǔ)之循環(huán)結(jié)構(gòu)語(yǔ)句詳解
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)法基礎(chǔ)之循環(huán)結(jié)構(gòu)語(yǔ)句,感興趣的小伙伴們可以參考一下2016-09-09
深入理解SpringBoot?最大連接數(shù)及最大并發(fā)數(shù)
SpringBoot能支持的最大并發(fā)量主要看其對(duì)Tomcat的設(shè)置,可以在配置文件中對(duì)其進(jìn)行更改,本文就來(lái)介紹一下SpringBoot?最大連接數(shù)及最大并發(fā)數(shù),感興趣的可以了解一下2023-08-08
Spring?Boot?實(shí)現(xiàn)字段唯一校驗(yàn)功能(實(shí)例代碼)
這篇文章主要介紹了Spring?Boot?實(shí)現(xiàn)字段唯一校驗(yàn),實(shí)現(xiàn)代碼很簡(jiǎn)單,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
SpringBoot查詢(xún)PGSQL分表后的數(shù)據(jù)的代碼示例
數(shù)據(jù)庫(kù)用的pgsql,在表數(shù)據(jù)超過(guò)100w條的時(shí)候執(zhí)行定時(shí)任務(wù)進(jìn)行了分表,分表后表名命名為原的表名后面拼接時(shí)間,但是我在java業(yè)務(wù)代碼中,我想查詢(xún)之前的那條數(shù)據(jù)就查不到了,本文給大家介紹了SpringBoot中如何查詢(xún)PGSQL分表后的數(shù)據(jù),需要的朋友可以參考下2024-05-05
SpringBoot Data JPA 關(guān)聯(lián)表查詢(xún)的方法
這篇文章主要介紹了SpringBoot Data JPA 關(guān)聯(lián)表查詢(xún)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java使用Spring Batch處理大規(guī)模數(shù)據(jù)的實(shí)踐分享
在處理大規(guī)模數(shù)據(jù)的場(chǎng)景中,批處理是一個(gè)非常常見(jiàn)且必要的操作,Java中的Spring Batch是一個(gè)強(qiáng)大的框架,能夠幫助我們高效地執(zhí)行復(fù)雜的批處理任務(wù),本文將帶大家了解如何使用Spring Batch處理大規(guī)模數(shù)據(jù),并通過(guò)代碼示例展示如何實(shí)現(xiàn)高效的批處理,需要的朋友可以參考下2024-10-10

