天天看點

java 配置參數_給你的JAVA程式配置參數(Properties的使用)

我們在寫JAVA程式時,很多時候運作程式的參數是需要動态改變的

測試時一系列參數,運作時一系列參數

又或者資料庫位址也需要配一套參數,以友善今後的動态部署

這些變量的初始化,我們在寫小DEMO時完全可以寫死在JAVA檔案中

但程式需要釋出或者局部部署時,這些參數就需要脫離程式代碼了

我們有多種存放參數的方式,比如資料庫、XML檔案又或者直接是txt檔案

現在介紹一種使用JAVA,簡單友善的參數讀取方式

.properties檔案,我們并不陌生,很多優秀的架構中就能看到它的存在,比如Hibernate

在src檔案目錄下,建立一個字尾為.properties的檔案,用任意文本編輯器打開它,就可以使用鍵值對的方式設定您程式的運作參數了

類似于這樣

#wangqun fd60e46db0dc119cfea740c3375fd7c4

#toAccountId=fd60e46db0dc119cfea740c3375fd7c4

#huangshi 1645a78135328c4b

toAccountId=1645a78135328c4b

#tangwei 6c0f7514f4bd0016

#shixiaoping 98f30bad9e6789af

#toAccountId=6c0f7514f4bd0016

#toAccountId=6c0f7514f4bd0016,1645a78135328c4b,98f30bad9e6789af

#cron=0 32 8-11,14-20/1 * * ?cron=0/10 * * * * ?fileName=d\:/\u59DC\u5830\u5468\u79EF\u5206\u7BA1\u63A7\u53CA\u53CC\u767E\u5146\u8BAD\u7EC3\u84250830.xlsx

sheetName=\u6C47\u603B

fromIndex=0,1toIndex=48,12#fromIndex=1,4#toIndex=13,12dirPath=C:/apache-tomcat-8.0.28/webapps/test/urlPath=http://61.132.43.176:8081/test/

database_host=132.240.9.36database_port=1521database_user=jy

database_name=yxdb

database_pwd=OVQwu8QSm4CWktEZdnjtxg==sql_str=select *from hs_bb_rbb r order by r.no

modify_sheet=sheet1

modify_from=1,0time_cell=1,4

注意一下幾點:

一、用#号表示注釋,可以多錄入一些配置可能,運作時動态注釋或者打開,比較友善

二、.properties明文存儲,是以敏感字元需要加密,比如資料的密碼

三、遇到中文,是個棘手的問題,最簡單的方式是使用MyEclipse的properties檔案編輯器來寫入,會自動将中文轉碼

四、所有參數隻可以以字元串形式存儲,至于類型轉換,請在JAVA中解析完成

五、适用于隻讀不寫的參數配置,如果程式運作過程中需要修改這些環境參數,建議考慮資料庫讀寫方式,而不是properties

下面是JAVA程式,mian函數一開始就可以對這個properties檔案進行讀取

public static void main(String[] args) throwsException{//讀取properties配置檔案

Properties prop=newProperties();

prop.load(QuartzDemo.class.getResourceAsStream("/set.properties"));String cron=prop.getProperty("cron","0/10 * 8-7 * * ?");String fileName=prop.getProperty("fileName","d:/2014年1月營銷活動報表140116.xlsx");

String sheetName=prop.getProperty("sheetName", "支局視圖");

String dirPath=prop.getProperty("dirPath","C:/apache-tomcat-8.0.28/webapps/test/");

String urlPath=prop.getProperty("urlPath","http://61.132.43.176:8081/test/");

String fromIndexStr=prop.getProperty("fromIndex","0,0");

String toIndexStr=prop.getProperty("toIndex", "17,20");

String database_host=prop.getProperty("database_host","");

String database_port=prop.getProperty("database_port","");

String database_user=prop.getProperty("database_user","");

String database_name=prop.getProperty("database_name","");

String database_pwd=DESHelper.decrypt(prop.getProperty("database_pwd",""),"newflypig");

String sql_str=prop.getProperty("sql_str","");

String modify_sheet=prop.getProperty("modify_sheet","");

String modify_fromStr=prop.getProperty("modify_from","0,0");

String[] splitStr=fromIndexStr.split(",");int[] fromIndex={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};

splitStr=toIndexStr.split(",");int[] toIndex={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};

splitStr=modify_fromStr.split(",");int[] modify_from={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};

}

Properties類提供了非常便捷的讀取properties檔案的操作,還包括一些預設值的配置,再次需要注意的是,隻能讀String,如果遇到其他資料類型,聰明的你一定知道怎樣用String類型轉換這些類型吧。