读取配置properties配置文件信息

package com.ailk.toptea.wa.assessment.util;

import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Properties;
import java.io.FileReader;

/**
 * 读取property的配置文件
 *
 */
public class Tools {
	/**
	 * 读取配置文件中的指定的key
	 * @param file 配置文件路径
	 * @param name 配置文件中的key
	 * @return
	 */
	public static String getProperty(String file, String name) {
		ResourceBundle rb = ResourceBundle.getBundle(file, Locale.getDefault());
		return rb.getString(name);
	}

	public static String str_replace(String from, String to, String source) {
		StringBuffer bf = new StringBuffer("");
		StringTokenizer st = new StringTokenizer(source, from, true);
		while (st.hasMoreTokens()) {
			String tmp = st.nextToken();
			if (tmp.equals(from)) {
				bf.append(to);
			} else {
				bf.append(tmp);
			}
		}
		return bf.toString();
	}
	
	/**
	 * 根据格式化的时间格式和改变类型获得时间
	 * @param type
	 * @param chgVal
	 * @param formatType
	 * @return
	 */
	public static String getDateByFormatType(int type,int chgVal, String formatType){
		//一个月内
    	Calendar cal = Calendar.getInstance();
    	java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(formatType);
    	if(type == 1){//日处理
    		cal.add(Calendar.DAY_OF_MONTH, chgVal);
    	}else if(type == 2){//月处理
    		cal.add(Calendar.MONTH, chgVal);
    	}else if(type == 3){//年处理
    		cal.add(Calendar.YEAR, chgVal);
    	}
 
        return format.format(cal.getTime()); 
	}
	
	public static void main(String[] args){
		System.out.println(Tools.getDateByFormatType(3, -1, "yyyy/MM/dd"));
		String str = Tools.getProperty("conf/nl-ini-param", "host");
		System.out.println(str+"======");
	}
	
	/**
	 * 动态获取配置文件信息
	 * @param properPth 配置文件路径
	 * @param keyname 要获取的key
	 * @return
	 */
	 public static String readProper(String properPth,String keyname){
		 Properties pro=new Properties();
		 FileReader fis= null;
		 String keyvalue = "";
		 try{
			 fis=new FileReader(properPth);
			 pro.load(fis);
			 fis.close();
			 keyvalue =  pro.getProperty(keyname);
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return keyvalue;
	 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值