www.pudn.com > network-java.rar > PropUtil.java
package netManager.util; /** *PropUtil
*Description: 配置文件的设定
*Copyright: Copyright (c) 2005
*Company:
* @author:谢飞 * @version 1.0 */ import java.util.Properties; import java.io.FileInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import netManager.framework.exception.BaseException; public class PropUtil{ private final static String URL = "DbUrl"; //数据库连接字符串 private final static String USERNAME = "username"; //数据库用户名 private final static String PASSWORD = "password"; //数据库密码 private final static String SUBNETMASK = "subnetmask"; //本机子网掩码 private Properties prop; private Log log = LogFactory.getLog("PropUtil"); public PropUtil(String propertiesFilename) throws BaseException{ File propFile = null; prop = new Properties(); //String path = System.getProperty("user.dir"); //propertiesFilename = path + propertiesFilename; propFile = new File(propertiesFilename); if (propFile.exists() == true){ try { FileInputStream in = new FileInputStream(propFile); prop.load(in); in.close(); //System.out.println("Util(): Using properties file '" + propFile + "'."); } catch (FileNotFoundException exc) { //System.out.println("Util(): FileNotFoundException " + exc.getMessage()); log.error(exc); } catch (IOException exc) { //System.out.println("Util(): IOException " + exc.getMessage()); log.error(exc); } } else{ System.out.println("Util(): Cannot find properties file '" + propFile + "'. "); BaseException be = new BaseException(); log.error(be); be.setMessageKey("error.propfile.notfind"); throw be; } } public String getDbUrl(){ String DbUrl = prop.getProperty(URL); return DbUrl; } public String getDbUserName(){ String DbUserName = prop.getProperty(USERNAME); return DbUserName; } public String getDbPassword(){ String subnetMask = prop.getProperty(PASSWORD); return subnetMask; } public String getSubnetMask(){ String SubnetMask = prop.getProperty(SUBNETMASK); return SubnetMask; } }