最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年09月07日 10:07:39   作者:暴脾氣大大  
這篇文章主要介紹了Java讀取.properties配置文件的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

介紹幾種讀取方式:

1、基于ClassLoder讀取配置文件

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

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

2、基于 InputStream 讀取配置文件

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

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

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

1>通過 ResourceBundle.getBundle() 靜態(tài)方法來獲?。≧esourceBundle是一個抽象類),這種方式來獲取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來獲?。┗騝om/test/config/config(通過ResourceBundle來獲?。粚傩晕募趕rc根目錄下,則直接使用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);
    }
  }

  /**
   * 根據(jù)key讀取value
   *
   * @Title: getProperties_1
   * @Description: 第一種方式:根據(jù)文件名使用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);
      // 根據(jù)關(guān)鍵字查詢相應(yīng)的值
      value = prop.getProperty(keyWord);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return value;
  }

  /**
   * 讀取配置文件所有信息
   *
   * @Title: getProperties_1
   * @Description: 第一種方式:根據(jù)文件名使用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();
    }
  }

  /**
   * 根據(jù)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);
      // 根據(jù)關(guān)鍵字獲取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();
    }
  }

  /**
   * 根據(jù)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");
  }
}

到此這篇關(guān)于Java讀取.properties配置文件的幾種方式的文章就介紹到這了,更多相關(guān)Java 讀取properties 配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java ArrayList與LinkedList及HashMap容器的用法區(qū)別

    Java ArrayList與LinkedList及HashMap容器的用法區(qū)別

    這篇文章主要介紹了Java ArrayList與LinkedList及HashMap容器的用法區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-07-07
  • Java新手入門學(xué)習(xí)之正則表達式

    Java新手入門學(xué)習(xí)之正則表達式

    這篇文章主要給大家介紹了關(guān)于Java新手入門學(xué)習(xí)之正則表達式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java實現(xiàn)SHA1加密代碼實例

    Java實現(xiàn)SHA1加密代碼實例

    這篇文章給大家分享了Java實現(xiàn)SHA1加密的相關(guān)實例代碼,有興趣的朋友可以測試參考下。
    2018-07-07
  • 詳細介紹Spring的配置文件

    詳細介紹Spring的配置文件

    這篇文章主要為大家詳細介紹了Spring中的配置文件的命名以及它的配置文件都有些什么。文中的示例代碼講解詳細,感興趣的小伙伴可以跟上小編一起學(xué)習(xí)一下
    2022-10-10
  • Springsession nginx反向代理集成過程

    Springsession nginx反向代理集成過程

    這篇文章主要介紹了Springsession nginx反向代理集成過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Java中springboot搭建html的操作代碼

    Java中springboot搭建html的操作代碼

    這篇文章主要介紹了Java中springboot搭建html的相關(guān)操作,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • jdk-logging?log4j?logback日志系統(tǒng)實現(xiàn)機制原理介紹

    jdk-logging?log4j?logback日志系統(tǒng)實現(xiàn)機制原理介紹

    這篇文章主要介紹了jdk-logging、log4j、logback日志介紹以及三個日志系統(tǒng)的實現(xiàn)機制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java使用正則獲取括號里面的內(nèi)容

    Java使用正則獲取括號里面的內(nèi)容

    這篇文章主要介紹了Java使用正則獲取括號里面的內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • idea遠程調(diào)試spark的步驟講解

    idea遠程調(diào)試spark的步驟講解

    今天小編就為大家分享一篇關(guān)于idea遠程調(diào)試spark的步驟講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 列舉java語言中反射的常用方法及實例代碼

    列舉java語言中反射的常用方法及實例代碼

    反射機制指的是程序在運行時能夠獲取自身的信息。這篇文章主要介紹了列舉java語言中反射的常用方法,需要的朋友可以參考下
    2019-07-07

最新評論

广州市| 夏津县| 茂名市| 青冈县| 池州市| 巍山| 常德市| 班戈县| 茶陵县| 阳信县| 修水县| 启东市| 福清市| 淄博市| 松滋市| 桃江县| 麻城市| 依安县| 玛纳斯县| 西吉县| 田林县| 新昌县| 微山县| 陕西省| 文水县| 金华市| 静宁县| 喀喇沁旗| 康平县| 南阳市| 双牌县| 太康县| 南澳县| 桃江县| 汤阴县| 崇礼县| 天台县| 迁安市| 新巴尔虎左旗| 岗巴县| 阿拉善左旗|