1.
?1?private?void?getContextParam(){
?2???InputStream?is=this.getClass().getClassLoader().getResourceAsStream("clientConfig.properties");
?3???Properties?prop=?new?Properties();
?4???try?{
?5????prop.load(is);
?6????this.classForName=prop.getProperty("classForName");
?7????this.connectStr=prop.getProperty("connectStr");
?8????this.userName=prop.getProperty("userName");
?9????this.psw=prop.getProperty("psw");
10???}?catch?(IOException?e)?{
11????e.printStackTrace();
12???}
13??}
14??//注:只能將clientConfig.properties放入src下
15?
16?
?2???InputStream?is=this.getClass().getClassLoader().getResourceAsStream("clientConfig.properties");
?3???Properties?prop=?new?Properties();
?4???try?{
?5????prop.load(is);
?6????this.classForName=prop.getProperty("classForName");
?7????this.connectStr=prop.getProperty("connectStr");
?8????this.userName=prop.getProperty("userName");
?9????this.psw=prop.getProperty("psw");
10???}?catch?(IOException?e)?{
11????e.printStackTrace();
12???}
13??}
14??//注:只能將clientConfig.properties放入src下
15?
16?
2.
?1?String?realName=this.getServletContext().getRealPath("WEB-INF");
?2??????Properties?prop=?new?Properties();
?3???InputStream?is=null;
?4???try?{
?5????is?=?new?BufferedInputStream(new?FileInputStream(new?File(realName+"/clientConfig.properties")));
?6????prop.load(is);
?7???}?catch?(FileNotFoundException?e)?{
?8????//logger.info("沒有發現所需要的配置文件!"+e);
?9????e.printStackTrace();
10???}?catch?(IOException?e)?{
11????//logger.info("讀取文件的過程中出錯!"+e);
12????e.printStackTrace();
13???}
14?
15?//注:同樣需要定制文件存放的目錄
16?
17?
?2??????Properties?prop=?new?Properties();
?3???InputStream?is=null;
?4???try?{
?5????is?=?new?BufferedInputStream(new?FileInputStream(new?File(realName+"/clientConfig.properties")));
?6????prop.load(is);
?7???}?catch?(FileNotFoundException?e)?{
?8????//logger.info("沒有發現所需要的配置文件!"+e);
?9????e.printStackTrace();
10???}?catch?(IOException?e)?{
11????//logger.info("讀取文件的過程中出錯!"+e);
12????e.printStackTrace();
13???}
14?
15?//注:同樣需要定制文件存放的目錄
16?
17?
3.
?1?public?static?String?getValue(String?key)?{
?2?
?3?private?static?Properties?propertie;
?4?
?5?String?path?=?(Configuration.class.getResource("/")?+?"").substring(6);
?6???Configuration?rc?=?new?Configuration(path?+?"//config.properties");
?7???if?(propertie.containsKey(key))?{
?8????String?value?=?propertie.getProperty(key);//?得到某一屬性的值
?9????return?value;
10???}?else
11????return?"";
12??}
13?
14?//注:只適用在windows下,不同的操作系統需要使用File.pathSeparatorChar進行分割
15?
?2?
?3?private?static?Properties?propertie;
?4?
?5?String?path?=?(Configuration.class.getResource("/")?+?"").substring(6);
?6???Configuration?rc?=?new?Configuration(path?+?"//config.properties");
?7???if?(propertie.containsKey(key))?{
?8????String?value?=?propertie.getProperty(key);//?得到某一屬性的值
?9????return?value;
10???}?else
11????return?"";
12??}
13?
14?//注:只適用在windows下,不同的操作系統需要使用File.pathSeparatorChar進行分割
15?