Java中验证编码格式的一种方法

 1 package forlittlecatty;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.UnsupportedEncodingException;
 8 import java.util.Arrays;
 9 
10 public class MainTest {
11 
12     public static void main(String[] args) {
13         byte[] b1 = readByte(new File("./src/main/java/forlittlecatty/iso8859-1_en")); // 文件编码: ISO-8859-1 文件内容: "abc123!@#$%^&*()"
14         byte[] b2 = readByte(new File("./src/main/java/forlittlecatty/utf-8_en_cn")); // 文件编码: UTF-8 文件内容: "abc123!@#$%^&*()小猫咪"
15         byte[] b3 = readByte(new File("./src/main/java/forlittlecatty/gbk_en")); // 文件编码: GBK 文件内容: "abc123!@#$%^&*()"
16         byte[] b4 = readByte(new File("./src/main/java/forlittlecatty/gbk_cn")); // 文件编码: GBK 文件内容: "小猫咪"
17         System.out.println(isUTF8(b1)); // true
18         System.out.println(isUTF8(b2)); // true
19         System.out.println(isUTF8(b3)); // true
20         System.out.println(isUTF8(b4)); // false
21     }
22 
23     private static byte[] readByte(File file) {
24         InputStream input = null;
25         byte[] bytes = null;
26         try {
27             input = new FileInputStream(file);
28             bytes = new byte[256];
29             input.read(bytes);
30         } catch (IOException e) {
31             e.printStackTrace();
32         } finally {
33             try {
34                 input.close();
35             } catch (IOException e) {
36                 e.printStackTrace();
37             }
38         }
39         return bytes;
40     }
41 
42     private static boolean isUTF8(byte[] src) {
43         byte[] tmp = null;
44         try {
45             tmp = new String(src, "UTF-8").getBytes("UTF-8");
46         } catch (UnsupportedEncodingException e) {
47             e.printStackTrace();
48         }
49         return Arrays.equals(src, tmp);
50     }
51 }

 

posted @ 2018-01-19 13:55  Storm_L  阅读(1080)  评论(0编辑  收藏  举报