1,转化文件编码
文件的原始编码是inputCharset,转为outputCharset的编码格式
- /***
- *
- * @param srcfile : source file
- * @param targfile : target file
- * @param inputCharset : from charset
- * @param outputCharset : to charset
- */
- public static void convertEncoding(File srcfile, File targfile,
- String inputCharset, String outputCharset) {
- FileInputStream fin = null;
- FileOutputStream fout = null;
- char[] cbuf = new char[BUFF_SIZE];
- int size_char;
- try {
- fin = new FileInputStream(srcfile);
- fout = new FileOutputStream(targfile);
- InputStreamReader isr = null;
- OutputStreamWriter osw = null;
- try {
- isr = new InputStreamReader(fin, inputCharset);
- osw = new OutputStreamWriter(fout, outputCharset);
- while ((size_char = isr.read(cbuf)) != NEGATIVE_ONE) {
- osw.write(cbuf, 0, size_char);
- }
- //
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- } catch (IOException e1) {
- e1.printStackTrace();
- } finally {
- try {
- isr.close();
- osw.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- } finally {
- try {
- if (fin != null) {
- fin.close();
- }
- if (fout != null) {
- fout.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
2,判断是否是中文字符(包括中文的标点符号,如。)
- /**
- * 字节数大于1,则返回true
- *
- * @param c
- * @return
- */
- public static boolean isChinese(char c) {
- return String.valueOf(c).getBytes().length > 1;
- }
3,获取文件的MD5值
- /**
- * Get MD5 of one file!
- *
- * @param file
- * @return
- */
- public static String getFileMD5(File file) {
- if (!file.exists() || !file.isFile()) {
- return null;
- }
- MessageDigest digest = null;
- FileInputStream in = null;
- byte buffer[] = new byte[1024];
- int len;
- try {
- digest = MessageDigest.getInstance("MD5");
- in = new FileInputStream(file);
- while ((len = in.read(buffer, 0, 1024)) != -1) {
- digest.update(buffer, 0, len);
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- BigInteger bigInt = new BigInteger(1, digest.digest());
- return bigInt.toString(16);
- }
4,获取实际类型的class
- /***
- * 获取实际的子类的class
- * @param clz
- * @return
- */
- public static <T> Class<T> getGenricClassType(
- @SuppressWarnings("rawtypes") Class clz) {
- Type type = clz.getGenericSuperclass();
- if (type instanceof ParameterizedType) {
- ParameterizedType pt = (ParameterizedType) type;
- Type[] types = pt.getActualTypeArguments();
- if (types.length > 0 && types[0] instanceof Class) {
- return (Class) types[0];
- }
- }
- return (Class) Object.class;
- }
应用:在dao层,GoodsDao 继承GenericDao,在GoodsDao 中就不用重复增删改查了。
- package com.common.dao.generic;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.SessionFactory;
- import org.hibernate.criterion.Example;
- import com.common.util.SystemUtil;
- /***
- * all dao must extends this class
- *
- * @author huangwei
- *
- * @param <T>
- */
- public abstract class GenericDao<T> {
- /***
- * generated by spring configuration file
- */
- protected SessionFactory sessionFactory;
- protected final Class<T> clz = SystemUtil.getGenricClassType(getClass());
- public SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- /***************************************************************/
- public void add(Object obj) {
- this.sessionFactory.getCurrentSession().save(obj);
- }
- public void save(Object obj) {
- this.add(obj);
- }
- public void delete(Object obj) {
- this.sessionFactory.getCurrentSession().delete(obj);
- }
- public void deleteById(int id) {
- this.sessionFactory.getCurrentSession().delete(get(id));
- }
- public T get(int id) {
- return (T) this.sessionFactory.getCurrentSession().get(clz, id);
- }
- public void update(Object obj) {
- this.sessionFactory.getCurrentSession().update(obj);
- }
- public List<T> getAll() {
- return (List<T>) this.sessionFactory.getCurrentSession()
- .createCriteria(clz).list();
- }
- /***
- * Find in DB depending on conditions
- *
- * @param obj
- * @return
- */
- public List<T> find(Object obj) {
- if (obj == null) {
- return this.getAll();
- } else {
- Example example = Example.create(obj).excludeZeroes();
- Criteria criteria = this.sessionFactory.getCurrentSession()
- .createCriteria(clz).add(example);
- return (List<T>)criteria.list();
- }
- }
- }
- package com.shop.jn.dao;
- import com.common.dao.generic.GenericDao;
- import com.shop.jn.entity.Goods;
- public class GoodsDao extends GenericDao<Goods> {
- }
5,合并字节数组
- /***
- * 合并字节数组
- * @param a
- * @return
- */
- public static byte[] mergeArray(byte[]... a) {
- // 合并完之后数组的总长度
- int index = 0;
- int sum = 0;
- for (int i = 0; i < a.length; i++) {
- sum = sum + a[i].length;
- }
- byte[] result = new byte[sum];
- for (int i = 0; i < a.length; i++) {
- int lengthOne = a[i].length;
- // 拷贝数组
- System.arraycopy(a[i], 0, result, index, lengthOne);
- index = index + lengthOne;
- }
- return result;
- }
6,判断关键字keyword 在srcText 中出现的次数
方式一:
- /**
- *
- * The number of occurrences of find keyword in srcText
- *
- * @param srcText
- * @param keyword
- * @return
- */
- public static int findStr1(String srcText, String keyword) {
- int count = 0;
- int leng = srcText.length();
- int j = 0;
- for (int i = 0; i < leng; i++) {
- if (srcText.charAt(i) == keyword.charAt(j)) {
- j++;
- if (j == keyword.length()) {
- count++;
- j = 0;
- }
- } else {
- i = i - j;// should rollback when not match
- j = 0;
- }
- }
- return count;
- }
方式二:
- public static int findStr2(String srcText, String keyword) {
- int count = 0;
- Pattern p = Pattern.compile(keyword);
- Matcher m = p.matcher(srcText);
- while (m.find()) {
- count++;
- }
- return count;
- }
7, 获取字节数组findTarget 在字节数组source中出现的位置
- /***
- *
- * @param source
- * @param findTarget
- * :key word
- * @param pos
- * :where start from
- * @return index
- */
- public static int findBytes(byte[] source, byte[] findTarget, int pos) {
- int i, j, k = 0;
- i = pos;
- j = 0;
- while (i < source.length && j < findTarget.length) {
- if (source[i] == findTarget[j]) {
- ++i;
- ++j;
- if (j == findTarget.length) {
- k = k + 1;// k++
- break;
- // j = 0;
- }
- } else {
- i = i - j + 1;
- j = 0;
- }
- }
- return k == 0 ? -1 : i - j;
- }
- /***
- * start from 0
- *
- * @param source
- * @param findTarget
- * @return
- */
- public static int findBytes(byte[] source, byte[] findTarget) {
- return findBytes(source, findTarget, 0);
- }