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

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

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都做網(wǎng)站、武都網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、武都網(wǎng)絡(luò)營銷、武都企業(yè)策劃、武都品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供武都建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:bm7419.com

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

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

代碼實現(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){
 
 }
}

運行結(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ù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享文章:Java讀取、獲取配置文件.properties中的數(shù)據(jù)
網(wǎng)站網(wǎng)址:http://bm7419.com/article30/igsopo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)頁設(shè)計公司、搜索引擎優(yōu)化、網(wǎng)站設(shè)計公司、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化