config读取工具类

package util.com.sf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 全局配置参数
 */
public class Config {
	private Config(){}
	/**
	 * 日志级别
	 */
	private static int loglevel = 2;
	/**
	 * 日志有效期,单位:天
	 */
	private static int logvalidityday = 7;
	/**
	 * 日志保存路径
	 */
	private static String logsavepath = ".";
	/**
	 * 日志对象
	 */
	private static CDcwLogFile log;
	/**
	 * 读配置值
	 * 
	 * @param name
	 */
	public static String getConfigValue(String key) {
		String str = null;
		FileInputStream fin=null;
		try {
		    fin = new FileInputStream("/config.properties");
			Properties prop = new Properties();
			prop.load(fin);
			str = prop.getProperty(key);
			fin.close();
		} catch (Exception e) {
			log.writeLog(2, "读写配置值失败:"+e);
		}finally{
			if(fin!=null){
				try {
					fin.close();
				} catch (IOException e) {
					log.writeLog(2, "读写配置值失败:"+e);
				}
			}
		}
		if (str == null) {
			str = "";
		}
		return str;
	}

	/**
	 * 写配置值
	 * 
	 * @param name
	 * @param val
	public static void setConfigValue(String key, String value) {
		FileInputStream fin=null;
		String str="/config.properties";
		try {
			File f = new File(str);
			if (!f.exists()&&f.createNewFile()) {
					log.writeLog(2, "创建文件成功:"+str);
			}
			 fin = new FileInputStream(f);
			Properties prop = new Properties();
			prop.load(fin);
			fin.close();
			prop.setProperty(key, value);
			FileOutputStream fout = new FileOutputStream(f);
			prop.store(fout, "save config");
			fout.close();
		} catch (Exception e) {
			log.writeLog(2, "写配置值失败:"+e);
		}finally{
			if(fin != null){
				try {
					fin.close();
				} catch (IOException e) {
					log.writeLog(2, "写配置值失败:"+e);
				}
			}
			
		}
	}
	 */

	/**
	 * 加载配置参数
	 */
	public static boolean load() {
		CDcwXmlFile xml = new CDcwXmlFile(System.getProperty("user.dir") + "/config/config.xml");
		boolean bRet = xml.selectSubItem("log");
		if (bRet) {
			loglevel = xml.readInt("level", 2);
			logvalidityday = xml.readInt("validityday", 7);
			logsavepath = xml.readStr("path");
			// 创建日志文件的目录
			try {
				File f = new File(logsavepath);
				if (!f.exists()) {
					f.mkdirs();
				}
			} catch (Exception e) {
				log.writeLog(2, "创建日志文件的目录路径失败:"+e);
			}
			xml.selectParent();
		}
		log = new CDcwLogFile("log");
		return bRet;
	}
	public static int getLogLevel() {
		return loglevel;
	}

	public static int getLogValidityDay() {
		return logvalidityday;
	}

	public static String getLogSavePath() {
		return logsavepath;
	}

	/**
	 * 显示配置信息
	 */
	public static void printConfigs() {
		log.writeLog(2, "日志级别:"+ loglevel);
		log.writeLog(2, "日志有效期天数:"+ logvalidityday);
		log.writeLog(2, "香港顺便柜---webservice接口应用启动成功!");
	}

	/**
	 * 显示配置信息
	 */
	public static void printConfigsFail() {
		log.writeLog(2, "香港顺便柜---webservice接口应用启动失败!");
	}
	public static CDcwLogFile getLog() {
		return log;
	}

	public static void setLog(CDcwLogFile log) {
		Config.log = log;
	}

}


parameter.properties

#Created by hk - www.guh-software.de
#Thu Dec 17 17:46:24 CST 2017


#香港顺便柜开关(0关闭 1开启)
hongkong=1
#数据库操作语句
select_sql=select des from tt_waybill_des_mid where waybill_no=?
insert_sql=insert into tt_waybill_des_mid (id,waybill_no,des,update_tm) values(seq_tt_waybill_des.nextval,?,?,?)
update_sql=update tt_waybill_des_mid set des=?, update_tm=? where waybill_no=?
#webserver连接地址
serviceIP=http://10.118.63.43:8089/webdis/service/HongKong

  

posted @ 2017-12-15 14:36  杯子茶壶  阅读(111)  评论(0)    收藏  举报