您的位置:首页 > 编程语言 > Java开发

Java获取配置文件参数工具类

2015-08-24 11:08 507 查看
package com.bo.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

/**
* 数据库访问配置文件各参数的获取
* 将配置文件pdf.properties放在 项目Src下
* fdriver =oracle.jdbc.driver.OracleDriver
furl =jdbc\:oracle\:thin\:@192.168.12.247\:1521\:oracle
fuser =hhris
fpassword =hhris
* @author YaoYuanBo
*
*/
public class DbConfig {
//数据库及server配置文件路径
private static final String ACTIONPATH = "pdf.properties";
private static DbConfig instance=null;

private String bo_fdriver=null;
private String bo_furl=null;
private String bo_fuser=null;
private String bo_fpassword=null;

private DbConfig(){}

public static String getActionpath() {
return ACTIONPATH;
}

public String getBo_fdriver() {
return bo_fdriver;
}

public String getBo_furl() {
return bo_furl;
}

public String getBo_fuser() {
return bo_fuser;
}

public String getBo_fpassword() {
return bo_fpassword;
}

public static DbConfig getInstance(){
if(instance==null){
instance= new DbConfig().getNewDbConfig();
}
return instance;
}

private DbConfig getNewDbConfig(){

DbConfig dc=new DbConfig();
Properties prop = new Properties();
String path=null;
FileInputStream fis=null;

try {
path = DbConfig.class.getClassLoader().getResource("").toURI().getPath();
fis = new FileInputStream(new File(path + ACTIONPATH));
prop.load(fis);
dc.bo_fdriver=prop.getProperty("fdriver");
dc.bo_furl=prop.getProperty("furl");
dc.bo_fuser=prop.getProperty("fuser");
dc.bo_fpassword=prop.getProperty("fpassword");

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return dc;
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: