public class CodeUtil {
public static final SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
public static String encode(String str)
{
if(str==null||str.length()<0)
{
return "";
}
String returnStr="";
int length=str.length();
for(int i=0;i<length;i++)
{
char a=str.charAt(i);
int b=a;
if(i!=(length-1))
returnStr+=b+"--";
else
returnStr+=b;
}
return returnStr;
}
public static String decode(String strCode)
{
//System.out.println("debug CodeUtil L26==>"+strCode);
if(strCode==null||strCode.trim().length()<=0)
{
return "";
}
String[] str=strCode.split("--");
String returnStr="";
if(str.length>0){
for(int i=0;i<str.length;i++)
{
int stint=Integer.parseInt(str[i]);
char st=(char) stint;
returnStr+=st;
}
// System.out.println("debug in CodeUtilL41==>"+returnStr);
return returnStr;
}else{
return "";
}
}
public static void main(String[]sdfsd)
{
String str="西湖";
String s=CodeUtil.encode(str);
System.out.println("encode=>"+s);
System.out.println(CodeUtil.decode(s));
MemCached mc = new MemCached();
MemcachService ms = new MemcachServiceImpl();
MemCachedClient mcc = new MemCachedClient();
List<Object[]> re = new ArrayList<Object[]>();
// re = ms.searchLuneceDataByKeyword("北京", "");
System.out.println("---------");
Object[] obj = (Object[]) mcc.get("6666");
System.out.println(obj);
System.out.println(obj[5]+"--101--"+obj[1]+" lineId="+obj[18]);
}
/**切分词方法
* 20090903 galen chinese keyword segment with MMapl
* @throws Exception
*/
public static String segmentKeyword(String keyword) throws Exception{
//keyword = CodeUtil.stringFilter(keyword);
Analyzer analyzer = new MMAnalyzer();
Reader r = new StringReader(keyword);
TokenFilter sf = (TokenFilter) analyzer.tokenStream("", r);
StringBuffer sb = new StringBuffer();
int i=0;
Token t;
while ((t = sf.next()) != null) {
sb.append(t.termBuffer()).append(" ");
}
return sb.toString();
}
/**
* 20091107 galen 清除掉所有特殊字符
* @throws Exception
*/
public static String stringFilter(String str) throws PatternSyntaxException {
// 只允许字母和数字
// String regEx = "[^a-zA-Z0-9]";
String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
/**
* 20091107 过滤 -和:
* @throws Exception
*/
public static String dateFilter(String str) throws PatternSyntaxException {
// 只允许字母和数字
// String regEx = "[^a-zA-Z0-9]";
String regEx="[-:]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
boolean flag = true;
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
public static String readValue(String propertiesNm,String key) {
String filePath = MemCached.class.getResource("").getPath();
int index = filePath.lastIndexOf("classes");
filePath = filePath.substring(0,index)+"classes/"+propertiesNm;
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 20091123 写入properties信息
* @param propertiesNm
* @param parameterName
* @param parameterValue
*/
public static void writeProperties(String propertiesNm,String parameterName,String parameterValue) {
Properties prop = new Properties();
String filePath ="";
try {
filePath = MemCached.class.getResource("").getPath();
int index = filePath.lastIndexOf("classes");
filePath = filePath.substring(0,index)+"classes/"+propertiesNm;
System.out.println("ffff--"+filePath);
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}
}