欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 讀取、獲取配置文件.properties中的數(shù)據(jù)

 更新時(shí)間:2018年09月17日 11:21:57   作者:nayi_224  
這篇文章主要介紹了Java 讀取、獲取配置文件.properties中的數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

java獲取配置文件.properties中的數(shù)據(jù),具體內(nèi)容如下所示:

方法太多,只寫一種比較簡單的。

 文件test1.properties內(nèi)容

test1 = 123;
test2=3211
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/test1.properties"));
    System.out.println(prop.get("test1"));

輸出

123;1

簡單封裝一下,完整代碼

package propertis.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Test {
  /**
   * @param args
   * @throws IOException 
   * @throws FileNotFoundException 
   */
  public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO Auto-generated method stub
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/test1.properties"));
    System.out.println(prop.get("test1"));
    System.out.println(ProUtil.getTest1Value("test1"));
    System.out.println(ProUtil.getTest1Value("test2"));
  }
}
class ProUtil{
  private static Properties prop = new Properties();
  static{
    try {
      prop.load(new FileInputStream("src/test1.properties"));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static Object getTest1Value(String key){
    return prop.get(key);
  }
}

輸出

123;
123;
321

下面看下Java 讀取Properties配置文件

方法:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("**.properties");
properties.load(in);
in.close();

配置文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=

代碼實(shí)現(xiàn):

import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesTest {
 private static final String PROPERTIES_NAME = "db.properties";
 public static String DB_DRIVER = null;
 public static String DB_URL = null;
 public static String DB_USER = null;
 public static String DB_PWD = null;
 
 static{
 FileInputStream in = null;
 try{
  Properties properties = new Properties();
  in = new FileInputStream(PROPERTIES_NAME);
  properties.load(in);
  DB_DRIVER = properties.getProperty("driver");
  DB_URL = properties.getProperty("url");
  DB_USER = properties.getProperty("username");
  DB_PWD = properties.getProperty("passworld");
  System.out.println("讀取配置信息成功!");
  showConfig();
 }catch(Exception e){
  e.printStackTrace();
  System.out.println("讀取配置信息失??!");
 }finally{
  if(in != null){
  try{
   in.close();
  }catch(Exception e){
   e.printStackTrace();
  }
  }
 }
 }
 
 private static void showConfig(){
 System.out.println("-----------------------配置信息-----------------");
 System.out.println("dirver: "+DB_DRIVER);
 System.out.println("url: "+DB_URL);
 System.out.println("user: "+DB_USER);
 System.out.println("passworld: "+DB_PWD);
 System.out.println("----------------------------------------------");
 }
 
 public static void main(String[] args){
 
 }
}

運(yùn)行結(jié)果:

讀取配置信息成功!

-----------------------配置信息-----------------
dirver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
user: root
passworld: null
----------------------------------------------

總結(jié)

以上所述是小編給大家介紹的Java 讀取、獲取配置文件.properties中的數(shù)據(jù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Spring集成PageHelper的簡單用法示例

    Spring集成PageHelper的簡單用法示例

    這篇文章主要介紹了Spring集成PageHelper的簡單用法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • spring使用redis操作key-value的示例代碼

    spring使用redis操作key-value的示例代碼

    這篇文章主要介紹了spring使用redis操作key-value的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 基于CXF搭建webService的實(shí)例講解

    基于CXF搭建webService的實(shí)例講解

    下面小編就為大家?guī)硪黄贑XF搭建webService的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 淺談Java8 判空新寫法

    淺談Java8 判空新寫法

    在開發(fā)過程中很多時(shí)候會(huì)遇到判空校驗(yàn),如果不做判空校驗(yàn)則會(huì)產(chǎn)生NullPointerException異常,本文就來介紹一下Java8 判空新寫法,感興趣的可以了解一下
    2021-09-09
  • SpringMVC的最小化配置說明

    SpringMVC的最小化配置說明

    這篇文章主要介紹了SpringMVC的最小化配置說明,Spring MVC是一個(gè)基于Java的Web框架,用于構(gòu)建靈活、高效的Web應(yīng)用程序,它采用了MVC的設(shè)計(jì)模式,將應(yīng)用程序的邏輯分為模型、視圖和控制器三個(gè)部分,以實(shí)現(xiàn)代碼的分離和重用,需要的朋友可以參考下
    2023-10-10
  • 關(guān)于springboot的跨域配置問題的解決方案

    關(guān)于springboot的跨域配置問題的解決方案

    這篇文章主要介紹了關(guān)于springboot的跨域配置問題,處理filter,spring?security等過濾器跨域問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 深入理解Swift中的Substring和String

    深入理解Swift中的Substring和String

    這篇文章主要給大家深入的介紹了Swift中Substring和String的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 實(shí)例講解Java HashSet

    實(shí)例講解Java HashSet

    這篇文章主要介紹了Java HashSet的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 一種求正整數(shù)冪的高效算法詳解

    一種求正整數(shù)冪的高效算法詳解

    本篇文章是對(duì)java中一種求正整數(shù)冪的高效算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • jpa多條件查詢重寫Specification的toPredicate方法

    jpa多條件查詢重寫Specification的toPredicate方法

    這篇文章主要介紹了多條件查詢重寫Specification的toPredicate方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論