文字列の符号化フォーマットを判断し、符号化を行う

1930 ワード


public class TranCharset {
    /**
     *         
     *
     * @param str
     * @return
     */
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }
    public static void main(String[] args){
    	System.out.println(getEncoding("CSS  "));
    }
}
public static void main2(String[] args) throws Exception{
        String desc="CSS  "; 
        try { 
           String b = new String(desc.getBytes("GBK"), "UTF-8");
           System.out.println(b);
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 

    }
}