国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

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

2024-07-14 08:42:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

方法太多,只寫(xiě)一種比較簡(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.Driverurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8username=rootpassword=

代碼實(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 什邡市| 始兴县| 彩票| 临沭县| 岢岚县| 错那县| 雷州市| 铁岭县| 赫章县| 漳平市| 苗栗市| 闵行区| 禄丰县| 镇巴县| 亚东县| 和硕县| 商洛市| 图木舒克市| 龙南县| 巴楚县| 巴塘县| 瑞安市| 枞阳县| 洞口县| 广平县| 安龙县| 南投县| 高碑店市| 洛扎县| 鸡西市| 普兰店市| 博湖县| 伊吾县| 呈贡县| 黔西县| 太仆寺旗| 农安县| 长垣县| 文昌市| 德安县| 扶绥县|