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;
}
}
读取配置properties配置文件信息
最新推荐文章于 2024-05-24 10:49:58 发布