java字符编码转换

来源:互联网 发布:网络机顶盒推荐 编辑:程序博客网 时间:2024/04/25 21:53
 
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.Reader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.io.Writer;
  7. import java.util.Properties;
  8. import java.io.FileInputStream;
  9. public class PublicFun {
  10.     /**
  11.      * 将字符串编码格式转成GB2312
  12.      * 
  13.      * @param str
  14.      * @return
  15.      */
  16.     public static String TranEncodeTOGB(String str) {
  17.         try {
  18.             String strEncode = PublicFun.getEncoding(str);
  19.             String temp = new String(str.getBytes(strEncode), "GB2312");
  20.             return temp;
  21.         } catch (java.io.IOException ex) {
  22.             return null;
  23.         }
  24.     }
  25.     /**
  26.      * 将字符串编码格式转成UTF8
  27.      * 
  28.      * @param str
  29.      * @return
  30.      */
  31.     public static String TranEncodeTOUTF(String str) {
  32.         try {
  33.             String strEncode = PublicFun.getEncoding(str);
  34.             String temp = new String(str.getBytes(strEncode), "UTF-8");
  35.             return temp;
  36.         } catch (java.io.IOException ex) {
  37.             return null;
  38.         }
  39.     }
  40.     /**
  41.      * 判断输入字符是否为gb2312的编码格式
  42.      * 
  43.      * @param c
  44.      *            输入字符
  45.      * @return 如果是gb2312返回真,否则返回假
  46.      */
  47.     public static boolean isGB2312(char c) {
  48.         Character ch = new Character(c);
  49.         String sCh = ch.toString();
  50.         try {
  51.             byte[] bb = sCh.getBytes("gb2312");
  52.             if (bb.length > 1) {
  53.                 return true;
  54.             }
  55.         } catch (java.io.UnsupportedEncodingException ex) {
  56.             return false;
  57.         }
  58.         return false;
  59.     }
  60.     /**
  61.      * 判断字符串的编码
  62.      * 
  63.      * @param str
  64.      * @return
  65.      */
  66.     public static String getEncoding(String str) {
  67.         String encode = "GB2312";
  68.         try {
  69.             if (str.equals(new String(str.getBytes(encode), encode))) {
  70.                 String s = encode;
  71.                 return s;
  72.             }
  73.         } catch (Exception exception) {
  74.         }
  75.         encode = "ISO-8859-1";
  76.         try {
  77.             if (str.equals(new String(str.getBytes(encode), encode))) {
  78.                 String s1 = encode;
  79.                 return s1;
  80.             }
  81.         } catch (Exception exception1) {
  82.         }
  83.         encode = "UTF-8";
  84.         try {
  85.             if (str.equals(new String(str.getBytes(encode), encode))) {
  86.                 String s2 = encode;
  87.                 return s2;
  88.             }
  89.         } catch (Exception exception2) {
  90.         }
  91.         encode = "GBK";
  92.         try {
  93.             if (str.equals(new String(str.getBytes(encode), encode))) {
  94.                 String s3 = encode;
  95.                 return s3;
  96.             }
  97.         } catch (Exception exception3) {
  98.         }
  99.         return "";
  100.     }
  101. }
原创粉丝点击