文字の符号化フォーマットを判断する方法

3286 ワード

 1 public class TranCharset {
 2     /**
 3      *  
 4      *
 5      * @param str
 6      * @return
 7      */
 8     public static String getEncoding(String str) {
 9         String encode = "GB2312";
10         try {
11             if (str.equals(new String(str.getBytes(encode), encode))) {
12                 String s = encode;
13                 return s;
14             }
15         } catch (Exception exception) {
16         }
17         encode = "ISO-8859-1";
18         try {
19             if (str.equals(new String(str.getBytes(encode), encode))) {
20                 String s1 = encode;
21                 return s1;
22             }
23         } catch (Exception exception1) {
24         }
25         encode = "UTF-8";
26         try {
27             if (str.equals(new String(str.getBytes(encode), encode))) {
28                 String s2 = encode;
29                 return s2;
30             }
31         } catch (Exception exception2) {
32         }
33         encode = "GBK";
34         try {
35             if (str.equals(new String(str.getBytes(encode), encode))) {
36                 String s3 = encode;
37                 return s3;
38             }
39         } catch (Exception exception3) {
40         }
41         return "";
42     }
43     public static void main(String[] args){
44         System.out.println(getEncoding("CSS "));
45     }
46 
47 }