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

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)容如下所示:

方法太多,只寫一種比較簡(jiǎn)單的。

 文件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

簡(jiǎn)單封裝一下,完整代碼

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)文章

  • SpringBoot?替換?if?的參數(shù)校驗(yàn)示例代碼

    SpringBoot?替換?if?的參數(shù)校驗(yàn)示例代碼

    Spring?Validation是對(duì)hibernate?validation的二次封裝,用于支持spring?mvc參數(shù)自動(dòng)校驗(yàn),接下來,我們以spring-boot項(xiàng)目為例,介紹Spring?Validation的使用,需要的朋友可以參考下
    2022-12-12
  • SpringMVC框架實(shí)現(xiàn)Handler處理器的三種寫法

    SpringMVC框架實(shí)現(xiàn)Handler處理器的三種寫法

    這篇文章主要介紹了SpringMVC框架實(shí)現(xiàn)Handler處理器的三種寫法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 關(guān)于mybatis mapper類注入失敗的解決方案

    關(guān)于mybatis mapper類注入失敗的解決方案

    這篇文章主要介紹了關(guān)于mybatis mapper類注入失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫

    SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • SpringBoot使用@Validated處理校驗(yàn)的方法步驟

    SpringBoot使用@Validated處理校驗(yàn)的方法步驟

    @Validated?注解的主要目的是啟用和利用?Spring?的驗(yàn)證框架,它可以用于類上也可以用于方法參數(shù)上,本文給大家介紹了SpringBoot使用@Validated優(yōu)雅的處理校驗(yàn)的方法步驟,通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • 詳解Java實(shí)現(xiàn)負(fù)載均衡的幾種算法代碼

    詳解Java實(shí)現(xiàn)負(fù)載均衡的幾種算法代碼

    本篇文章主要介紹了詳解Java實(shí)現(xiàn)負(fù)載均衡的幾種算法代碼 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Java源碼解析Integer方法解讀

    Java源碼解析Integer方法解讀

    這篇文章主要介紹了Java源碼解析Integer方法解讀,包括toString方法、toUnsignedString方法、highestOneBit方法等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 詳解在SpringBoot應(yīng)用中獲取應(yīng)用上下文方法

    詳解在SpringBoot應(yīng)用中獲取應(yīng)用上下文方法

    本篇文章主要介紹了詳解在SpringBoot應(yīng)用中獲取應(yīng)用上下文方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • SpringBoot在 POM 中引入本地 JAR 包的方法

    SpringBoot在 POM 中引入本地 JAR 包的方法

    在開發(fā) Spring Boot 應(yīng)用程序時(shí),您可能需要使用本地 JAR 包來添加自定義庫或功能,本文將介紹在 Spring Boot 項(xiàng)目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • 深入解析Java編程中面向字節(jié)流的一些應(yīng)用

    深入解析Java編程中面向字節(jié)流的一些應(yīng)用

    這篇文章主要介紹了Java編程中面向字節(jié)流的一些應(yīng)用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-10-10

最新評(píng)論

永川市| 手游| 汝城县| 建阳市| 高阳县| 阿巴嘎旗| 东安县| 延安市| 韶山市| 寿光市| 宜丰县| 武清区| 邵东县| 临猗县| 通化县| 祁阳县| 伊宁县| 淮南市| 年辖:市辖区| 西充县| 三原县| 若尔盖县| 杭锦后旗| 泊头市| 赣榆县| 九台市| 临猗县| 靖边县| 伊金霍洛旗| 体育| 抚州市| 马关县| 舟曲县| 依兰县| 孟连| 蒙自县| 惠东县| 镇赉县| 灵台县| 东源县| 宁国市|