黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java 讀取 .properties 配置文件的幾種方式

Java 讀取 .properties 配置文件的幾種方式

更新時(shí)間:2019-09-01 09:00:00 來源:動(dòng)力節(jié)點(diǎn) 瀏覽4433次

 

最近有好多小伙伴們問小編properties 配置文件的幾種方式是什么?今天動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)專門針對(duì)此問題對(duì)大家進(jìn)行介紹,希望此文能夠幫助到更多java零基礎(chǔ)學(xué)員,下面就隨小編看看properties 配置文件的幾種方式?

  

Java 開發(fā)中,需要將一些易變的配置參數(shù)放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通過 DOM 或 SAX 方式解析,而讀取 properties 配置文件就比較容易。


  介紹幾種讀取方式:


  1、基于ClassLoder讀取配置文件


  注意:該方式只能讀取類路徑下的配置文件,有局限但是如果配置文件在類路徑下比較方便。


 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、通過 java.util.ResourceBundle 類來讀取,這種方式比使用 Properties 要方便一些


 ?。?)通過 ResourceBundle.getBundle() 靜態(tài)方法來獲取(ResourceBundle是一個(gè)抽象類),這種方式來獲取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);


注意:在使用中遇到的最大的問題可能是配置文件的路徑問題,如果配置文件入在當(dāng)前類所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,則要使用com/test/config/config.properties(通過Properties來獲取)或com/test/config/config(通過ResourceBundle來獲取);屬性文件在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中的工具類進(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 {

// 通過Spring中的PropertiesLoaderUtils工具類進(jìn)行獲取

prop = PropertiesLoaderUtils.loadAllProperties(filePath);

// 根據(jù)關(guān)鍵字查詢相應(yīng)的值

value = prop.getProperty(keyWord);

} catch (IOException e) {

e.printStackTrace();

}

return value;

}


/**

* 讀取配置文件所有信息

*

* @Title: getProperties_1

* @Description: 第一種方式:根據(jù)文件名使用Spring中的工具類進(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 {

// 通過Spring中的PropertiesLoaderUtils工具類進(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)目下的目錄開始計(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 {

// 通過輸入緩沖流進(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)目下的目錄開始計(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 {

// 通過輸入緩沖流進(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) {

// 注意路徑問題

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("*********************************************");

// 注意路徑問題

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("*********************************************");

// 注意路徑問題

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");

}

}


以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編介紹的“Java 讀取 .properties 配置文件的幾種方式”的內(nèi)容,希望能夠幫助到大家,更多java最新資訊請(qǐng)繼續(xù)關(guān)注動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)官網(wǎng)。


相關(guān)免費(fèi)視頻教程推薦


java properties視頻教程下載;http://www.ilovecolors.com.cn/xiazai/2494.html



提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产成人综合网 | 欧美精品导航 | 日本欧美一区二区 | 午夜yy6080在线观看福利片 | 国产 中文 制服丝袜 另类 | 日本一区二区在线不卡 | 特黄aaaaaaaaa及毛片 | 欧美性猛交xxxx免费看手交 | 99视频网址 | 精品一区二区在线欧美日韩 | 国产黄色a三级三级三级 | 播放欧亚一级特黄录像 | 日本妇人成熟免费观看18 | 精品91麻豆免费免费国产在线 | 国产成人综合亚洲亚洲欧美 | 亚洲a站| 亚洲欧美日韩中另类在线 | 久久精品五月天 | 久久91久久91精品免费观看 | 国内精品综合九九久久精品 | 日本高清三区 | 免费在线h视频 | 国产成人精品福利网站在线 | 在线观看国产剧情麻豆精品 | 天天靠天天擦天天摸 | 日本高清一级片 | 午夜剧院官方 | 日本三级黄在线观看 | 日韩在线高清视频 | 色在线视频播放 | 免费黄色一级毛片 | 成人福利在线视频免费观看 | 午夜性爽爽爽 | 国产成人综合亚洲一区 | 欧美成人免费全部观看在线看 | 亚洲图片视频在线观看 | 张柏芝国产一区在线观看 | 亚洲一区二区福利视频 | 日韩毛片免费看 | 开心激情婷婷 | 狂野猛交xxxx吃奶 |