日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:24日期:2022-08-25 13:46:18

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

介紹幾種讀取方式:

1、基于ClassLoder讀取配置文件

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

Properties properties = new Properties(); // 使用ClassLoader加載properties配置文件生成對應的輸入流 InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream('config/config.properties'); // 使用properties對象加載輸入流 properties.load(in); //獲取key對應的value值 properties.getProperty(String key);

2、基于 InputStream 讀取配置文件

注意:該方式的優點在于可以讀取任意路徑下的配置文件

Properties properties = new Properties(); // 使用InPutStream流讀取properties文件 BufferedReader bufferedReader = new BufferedReader(new FileReader('E:/config.properties')); properties.load(bufferedReader); // 獲取key對應的value值 properties.getProperty(String key);

3、通過 java.util.ResourceBundle 類來讀取,這種方式比使用 Properties 要方便一些

1>通過 ResourceBundle.getBundle() 靜態方法來獲取(ResourceBundle是一個抽象類),這種方式來獲取properties屬性文件不需要加.properties后綴名,只需要文件名即可

properties.getProperty(String key); //config為屬性文件名,放在包com.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);

注意:在使用中遇到的最大的問題可能是配置文件的路徑問題,如果配置文件入在當前類所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,則要使用com/test/config/config.properties(通過Properties來獲取)或com/test/config/config(通過ResourceBundle來獲取);屬性文件在src根目錄下,則直接使用config.properties或config即可。

下面附上幾種方式的測試代碼,僅供參考:

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); } } /** * 根據key讀取value * * @Title: getProperties_1 * @Description: 第一種方式:根據文件名使用spring中的工具類進行解析 * filePath是相對路勁,文件需在classpath目錄下 * 比如:config.properties在包com.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工具類進行獲取 prop = PropertiesLoaderUtils.loadAllProperties(filePath); // 根據關鍵字查詢相應的值 value = prop.getProperty(keyWord); } catch (IOException e) { e.printStackTrace(); } return value; } /** * 讀取配置文件所有信息 * * @Title: getProperties_1 * @Description: 第一種方式:根據文件名使用Spring中的工具類進行解析 * filePath是相對路勁,文件需在classpath目錄下 * 比如:config.properties在包com.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工具類進行獲取 prop = PropertiesLoaderUtils.loadAllProperties(filePath); printAllProperty(prop); } catch (IOException e) { e.printStackTrace(); } } /** * 根據key讀取value * * @Title: getProperties_2 * @Description: 第二種方式:使用緩沖輸入流讀取配置文件,然后將其加載,再按需操作 * 絕對路徑或相對路徑, 如果是相對路徑,則從當前項目下的目錄開始計算, * 如:當前項目路徑/config/config.properties, * 相對路徑就是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 { // 通過輸入緩沖流進行讀取配置文件 InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); // 加載輸入流 prop.load(InputStream); // 根據關鍵字獲取value值 value = prop.getProperty(keyWord); } catch (Exception e) { e.printStackTrace(); } return value; } /** * 讀取配置文件所有信息 * * @Title: getProperties_2 * @Description: 第二種方式:使用緩沖輸入流讀取配置文件,然后將其加載,再按需操作 * 絕對路徑或相對路徑, 如果是相對路徑,則從當前項目下的目錄開始計算, * 如:當前項目路徑/config/config.properties, * 相對路徑就是config/config.properties * * @param filePath * @return void * @throws */ public static void getProperties_2(String filePath){ Properties prop = new Properties(); try { // 通過輸入緩沖流進行讀取配置文件 InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); // 加載輸入流 prop.load(InputStream); printAllProperty(prop); } catch (Exception e) { e.printStackTrace(); } } /** * 根據key讀取value * * @Title: getProperties_3 * @Description: 第三種方式: * 相對路徑, properties文件需在classpath目錄下, * 比如:config.properties在包com.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: 第三種方式: * 相對路徑, properties文件需在classpath目錄下, * 比如:config.properties在包com.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'); }}

到此這篇關于Java讀取.properties配置文件的幾種方式的文章就介紹到這了,更多相關Java 讀取properties 配置內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲精品日本| 在线综合亚洲| 91欧美极品| 日本麻豆一区二区三区视频| 日本电影久久久| 日韩精彩视频在线观看| 伊人久久成人| 日韩中文字幕不卡| 日韩高清一区在线| 国产剧情在线观看一区| 国产精品激情电影| 九九久久国产| 午夜av成人| 欧美日韩国产高清| 伊人国产精品| 奇米色欧美一区二区三区| 国产精品久久国产愉拍| 首页国产精品| 激情欧美丁香| 蜜臀久久99精品久久久久宅男 | 1024精品久久久久久久久| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 午夜国产欧美理论在线播放| 亚洲一区二区三区免费在线观看| 久久夜色精品| 国产精品美女久久久久久不卡| 国产精品sss在线观看av| 成人国产精品一区二区网站| 神马午夜久久| 丝袜亚洲精品中文字幕一区| 亚洲一二av| 欧美成人精品午夜一区二区| 日韩av一级| 亚洲青青久久| 国际精品欧美精品| 亚洲欧洲一区二区天堂久久| 日韩毛片一区| 国产成人免费| 亚洲免费婷婷| 你懂的国产精品| 久久亚洲国产| 日产欧产美韩系列久久99| 日本久久黄色| 影音先锋久久精品| 日产精品一区二区| 狠狠色狠狠色综合日日tαg| 欧美日韩网址| 国产一区观看| 欧美成人精品一级| 黄色日韩在线| 老司机精品在线| 亚洲欧洲一区二区天堂久久| 国产精品啊v在线| 亚洲一区观看| 国产一区二区三区四区大秀| 亚洲视频电影在线| 国产精品麻豆久久| 亚洲精品自拍| 日韩不卡免费高清视频| 日本不卡一二三区黄网| 免费高潮视频95在线观看网站| 老牛影视一区二区三区| 国产精品不卡| 91精品国产自产观看在线 | 五月国产精品| 日韩三区免费| 国产图片一区| 在线亚洲观看| 欧美一级鲁丝片| 欧美在线日韩| 国产国产精品| 成人亚洲精品| 涩涩涩久久久成人精品| 中文字幕系列一区| 国产精品调教视频| 日韩中文欧美在线| 神马午夜在线视频| 欧美成人aaa| 日韩超碰人人爽人人做人人添| 精品亚洲美女网站| 国产精品3区| 日本综合精品一区| 国产精品日本| 尤物tv在线精品| 久久只有精品| 日本免费在线视频不卡一不卡二| av亚洲免费| 岛国av免费在线观看| 久久精品 人人爱| 亚洲一区二区三区中文字幕在线观看| 深夜视频一区二区| 国产精品2区| 日韩av中文字幕一区| 国产视频一区免费看| 免费在线小视频| 国产一区二区三区四区五区传媒| 91福利精品在线观看| 亚洲一区二区三区四区五区午夜| 久久久一二三| 成人福利av| 日韩不卡一区| 精品久久97| 国产精一区二区| 亚洲免费观看高清完整版在线观| 欧美在线亚洲| 亚洲精品电影| 尹人成人综合网| 精品一区毛片| 久久久久国产| 日韩不卡视频在线观看| 最近高清中文在线字幕在线观看1| 麻豆国产精品| 国产一区精品福利| 成人在线观看免费视频| 国产精品magnet| 欧美激情视频一区二区三区在线播放| 欧美日本精品| 国产精品视频3p| 日韩不卡一区二区| 91亚洲精品视频在线观看| 日本不卡一区二区三区| 91麻豆精品激情在线观看最新| 日本成人在线视频网站| 日韩国产欧美视频| 欧美视频一区| 美女视频黄久久| 精品一区视频| 日韩精品dvd| 欧美+日本+国产+在线a∨观看| 国内精品福利| 蜜臀va亚洲va欧美va天堂| 三级亚洲高清视频| 亚洲精品伦理| 中文字幕亚洲影视| 欧美天堂在线| 久久97视频| 日韩黄色大片| 婷婷色综合网| 中文字幕一区二区三区日韩精品 | 日韩中文字幕无砖| 国产视频一区二| 久久99视频| 亚洲精品福利电影| 在线看片不卡| 久久一二三区| 国产日韩在线观看视频| 国产精品日韩精品在线播放| 精品日产乱码久久久久久仙踪林| 成人在线免费观看网站| 黄色在线网站噜噜噜| 亚洲精品一二三区区别| 亚洲精品极品| 麻豆视频一区| 99精品视频精品精品视频| 久久成人国产| 欧美亚洲三级| 欧美aa一级| 蜜桃一区二区三区在线观看| 国产欧美在线| 高清精品久久| 国产精品嫩草99av在线| 清纯唯美亚洲综合一区| 成人午夜在线| 香蕉久久夜色精品国产| 国产美女久久| 日韩精品久久久久久久电影99爱| 亚洲视频播放| 欧美日韩亚洲一区| 国产亚洲一区二区手机在线观看| 亚洲欧美日韩专区| 国产精品久一| 国产高清一区二区| 国产欧美日韩精品高清二区综合区 | 亚州欧美在线| 国产资源在线观看入口av| 国产视频一区在线观看一区免费| 欧美视频精品全部免费观看| 中文字幕人成乱码在线观看| 视频一区二区三区在线| 免费日韩成人| 婷婷久久一区| 久久wwww| 免费精品国产| 国产精品videossex久久发布 | 久久精品青草| 欧美日韩伊人| 激情视频一区二区三区| 欧美午夜三级| 国产一区日韩欧美| 国产精品chinese| 国产综合精品一区| 国产精品丝袜在线播放| 好吊视频一区二区三区四区| 你懂的亚洲视频| 美国欧美日韩国产在线播放| 另类专区亚洲| 亚洲精一区二区三区| 黄色在线网站噜噜噜| 日本不卡视频在线| 久久久久久久久丰满|