java 常用方法

简介:

1,转化文件编码

文件的原始编码是inputCharset,转为outputCharset的编码格式

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param srcfile  : source file 
  4.      * @param targfile : target file 
  5.      * @param inputCharset  : from charset 
  6.      * @param outputCharset : to charset 
  7.      */  
  8.     public static void convertEncoding(File srcfile, File targfile,  
  9.             String inputCharset, String outputCharset) {  
  10.         FileInputStream fin = null;  
  11.         FileOutputStream fout = null;  
  12.         char[] cbuf = new char[BUFF_SIZE];  
  13.         int size_char;  
  14.         try {  
  15.             fin = new FileInputStream(srcfile);  
  16.             fout = new FileOutputStream(targfile);  
  17.             InputStreamReader isr = null;  
  18.             OutputStreamWriter osw = null;  
  19.             try {  
  20.                 isr = new InputStreamReader(fin, inputCharset);  
  21.                 osw = new OutputStreamWriter(fout, outputCharset);  
  22.                 while ((size_char = isr.read(cbuf)) != NEGATIVE_ONE) {  
  23.                     osw.write(cbuf, 0, size_char);  
  24.                 }  
  25.                 //  
  26.             } catch (UnsupportedEncodingException e1) {  
  27.                 e1.printStackTrace();  
  28.             } catch (IOException e1) {  
  29.                 e1.printStackTrace();  
  30.             } finally {  
  31.                 try {  
  32.                     isr.close();  
  33.                     osw.close();  
  34.                 } catch (IOException e1) {  
  35.                     e1.printStackTrace();  
  36.                 }  
  37.   
  38.             }  
  39.   
  40.         } catch (FileNotFoundException e1) {  
  41.             e1.printStackTrace();  
  42.         } finally {  
  43.             try {  
  44.                 if (fin != null) {  
  45.                     fin.close();  
  46.                 }  
  47.                 if (fout != null) {  
  48.                     fout.close();  
  49.                 }  
  50.             } catch (IOException e1) {  
  51.                 e1.printStackTrace();  
  52.             }  
  53.         }  
  54.     }  

 2,判断是否是中文字符(包括中文的标点符号,如。)

Java代码   收藏代码
  1. /** 
  2.      * 字节数大于1,则返回true 
  3.      *  
  4.      * @param c 
  5.      * @return 
  6.      */  
  7.     public static boolean isChinese(char c) {  
  8.         return String.valueOf(c).getBytes().length > 1;  
  9.     }  

 

3,获取文件的MD5值

Java代码   收藏代码
  1. /** 
  2.      * Get MD5 of one file! 
  3.      *  
  4.      * @param file 
  5.      * @return 
  6.      */  
  7.     public static String getFileMD5(File file) {  
  8.         if (!file.exists() || !file.isFile()) {  
  9.             return null;  
  10.         }  
  11.         MessageDigest digest = null;  
  12.         FileInputStream in = null;  
  13.         byte buffer[] = new byte[1024];  
  14.         int len;  
  15.         try {  
  16.             digest = MessageDigest.getInstance("MD5");  
  17.             in = new FileInputStream(file);  
  18.             while ((len = in.read(buffer, 01024)) != -1) {  
  19.                 digest.update(buffer, 0, len);  
  20.             }  
  21.             in.close();  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.             return null;  
  25.         }  
  26.         BigInteger bigInt = new BigInteger(1, digest.digest());  
  27.         return bigInt.toString(16);  
  28.     }  

 

4,获取实际类型的class

Java代码   收藏代码
  1. /*** 
  2.      * 获取实际的子类的class 
  3.      * @param clz 
  4.      * @return 
  5.      */  
  6.     public static <T> Class<T> getGenricClassType(  
  7.             @SuppressWarnings("rawtypes") Class clz) {  
  8.         Type type = clz.getGenericSuperclass();  
  9.         if (type instanceof ParameterizedType) {  
  10.             ParameterizedType pt = (ParameterizedType) type;  
  11.             Type[] types = pt.getActualTypeArguments();  
  12.             if (types.length > 0 && types[0instanceof Class) {  
  13.                 return (Class) types[0];  
  14.             }  
  15.         }  
  16.         return (Class) Object.class;  
  17.     }  

 应用:在dao层,GoodsDao 继承GenericDao,在GoodsDao 中就不用重复增删改查了。

Java代码   收藏代码
  1. package com.common.dao.generic;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Criteria;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.criterion.Example;  
  8.   
  9. import com.common.util.SystemUtil;  
  10. /*** 
  11.  * all dao must extends this class 
  12.  *  
  13.  * @author huangwei 
  14.  * 
  15.  * @param <T> 
  16.  */  
  17. public abstract class GenericDao<T> {  
  18.     /*** 
  19.      * generated by spring configuration file 
  20.      */  
  21.     protected SessionFactory sessionFactory;  
  22.     protected final Class<T> clz = SystemUtil.getGenricClassType(getClass());  
  23.   
  24.     public SessionFactory getSessionFactory() {  
  25.         return sessionFactory;  
  26.     }  
  27.   
  28.     public void setSessionFactory(SessionFactory sessionFactory) {  
  29.         this.sessionFactory = sessionFactory;  
  30.     }  
  31.   
  32.     /***************************************************************/  
  33.     public void add(Object obj) {  
  34.         this.sessionFactory.getCurrentSession().save(obj);  
  35.     }  
  36.   
  37.     public void save(Object obj) {  
  38.         this.add(obj);  
  39.     }  
  40.   
  41.     public void delete(Object obj) {  
  42.         this.sessionFactory.getCurrentSession().delete(obj);  
  43.     }  
  44.   
  45.     public void deleteById(int id) {  
  46.         this.sessionFactory.getCurrentSession().delete(get(id));  
  47.     }  
  48.   
  49.     public T get(int id) {  
  50.         return (T) this.sessionFactory.getCurrentSession().get(clz, id);  
  51.     }  
  52.   
  53.     public void update(Object obj) {  
  54.         this.sessionFactory.getCurrentSession().update(obj);  
  55.     }  
  56.   
  57.     public List<T> getAll() {  
  58.         return (List<T>) this.sessionFactory.getCurrentSession()  
  59.                 .createCriteria(clz).list();  
  60.     }  
  61.   
  62.     /*** 
  63.      * Find in DB depending on conditions 
  64.      *  
  65.      * @param obj 
  66.      * @return 
  67.      */  
  68.     public List<T> find(Object obj) {  
  69.         if (obj == null) {  
  70.             return this.getAll();  
  71.         } else {  
  72.             Example example = Example.create(obj).excludeZeroes();  
  73.             Criteria criteria = this.sessionFactory.getCurrentSession()  
  74.                     .createCriteria(clz).add(example);  
  75.             return (List<T>)criteria.list();  
  76.         }  
  77.     }  
  78.   
  79. }  

 

Java代码   收藏代码
  1. package com.shop.jn.dao;  
  2.   
  3. import com.common.dao.generic.GenericDao;  
  4. import com.shop.jn.entity.Goods;  
  5.   
  6. public class GoodsDao extends GenericDao<Goods> {  
  7. }  

5,合并字节数组

Java代码   收藏代码
  1. /*** 
  2.      * 合并字节数组 
  3.      * @param a 
  4.      * @return 
  5.      */  
  6.     public static byte[] mergeArray(byte[]... a) {  
  7.         // 合并完之后数组的总长度  
  8.         int index = 0;  
  9.         int sum = 0;  
  10.         for (int i = 0; i < a.length; i++) {  
  11.             sum = sum + a[i].length;  
  12.         }  
  13.         byte[] result = new byte[sum];  
  14.         for (int i = 0; i < a.length; i++) {  
  15.             int lengthOne = a[i].length;  
  16.             // 拷贝数组  
  17.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  18.             index = index + lengthOne;  
  19.         }  
  20.         return result;  
  21.     }  

 

6,判断关键字keyword 在srcText 中出现的次数

方式一:

Java代码   收藏代码
  1. /** 
  2.      *  
  3.      * The number of occurrences of find keyword in srcText 
  4.      *  
  5.      * @param srcText 
  6.      * @param keyword 
  7.      * @return 
  8.      */  
  9.     public static int findStr1(String srcText, String keyword) {  
  10.         int count = 0;  
  11.         int leng = srcText.length();  
  12.         int j = 0;  
  13.         for (int i = 0; i < leng; i++) {  
  14.             if (srcText.charAt(i) == keyword.charAt(j)) {  
  15.                 j++;  
  16.                 if (j == keyword.length()) {  
  17.                     count++;  
  18.                     j = 0;  
  19.                 }  
  20.             } else {  
  21.                 i = i - j;// should rollback when not match  
  22.                 j = 0;  
  23.             }  
  24.         }  
  25.   
  26.         return count;  
  27.     }  

 方式二:

Java代码   收藏代码
  1. public static int findStr2(String srcText, String keyword) {  
  2.         int count = 0;  
  3.         Pattern p = Pattern.compile(keyword);  
  4.         Matcher m = p.matcher(srcText);  
  5.         while (m.find()) {  
  6.             count++;  
  7.         }  
  8.         return count;  
  9.     }  

 

 

7, 获取字节数组findTarget 在字节数组source中出现的位置

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param source 
  4.      * @param findTarget 
  5.      *            :key word 
  6.      * @param pos 
  7.      *            :where start from 
  8.      * @return index 
  9.      */  
  10.     public static int findBytes(byte[] source, byte[] findTarget, int pos) {  
  11.         int i, j, k = 0;  
  12.         i = pos;  
  13.         j = 0;  
  14.         while (i < source.length && j < findTarget.length) {  
  15.             if (source[i] == findTarget[j]) {  
  16.                 ++i;  
  17.                 ++j;  
  18.                 if (j == findTarget.length) {  
  19.                     k = k + 1;// k++  
  20.                     break;  
  21.                     // j = 0;  
  22.                 }  
  23.             } else {  
  24.                 i = i - j + 1;  
  25.                 j = 0;  
  26.             }  
  27.         }  
  28.         return k == 0 ? -1 : i - j;  
  29.     }  
  30.   
  31.     /*** 
  32.      * start from 0 
  33.      *  
  34.      * @param source 
  35.      * @param findTarget 
  36.      * @return 
  37.      */  
  38.     public static int findBytes(byte[] source, byte[] findTarget) {  
  39.         return findBytes(source, findTarget, 0);  
  40.     }  

 

相关文章
|
Java
java实现遍历树形菜单方法——OpenSessionView实现
java实现遍历树形菜单方法——OpenSessionView实现
11 0
|
30天前
|
Java
java实现遍历树形菜单方法——TreeAction实现
java实现遍历树形菜单方法——TreeAction实现
9 0
|
30天前
|
Java
java实现遍历树形菜单方法——HibernateUtil实现
java实现遍历树形菜单方法——HibernateUtil实现
10 0
|
30天前
|
Java
java实现遍历树形菜单方法——service层
java实现遍历树形菜单方法——service层
11 0
|
30天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
30天前
|
Java
java实现遍历树形菜单方法——实体类VoteTree
java实现遍历树形菜单方法——实体类VoteTree
11 0
|
Java
java实现遍历树形菜单方法——index.jsp实现
java实现遍历树形菜单方法——index.jsp实现
6 0
|
9天前
|
Java
Java中ReentrantLock中tryLock()方法加锁分析
Java中ReentrantLock中tryLock()方法加锁分析
11 0
|
29天前
|
Java
java中日期处理的一些工具方法
java中日期处理的一些工具方法
17 1
|
1天前
|
Java
Java接口中可以定义哪些方法?
【4月更文挑战第13天】
4 0
Java接口中可以定义哪些方法?