判断文本内容编码格式

//获取编码格式 gb2312,UTF-16,UTF-8,Unicode,UTF-8
public
static String getCode(String path) throws Exception { InputStream inputStream = new FileInputStream(path); byte[] head = new byte[3]; inputStream.read(head); String code = "GBK"; // 或gb2312 if (head[0] == -1 && head[1] == -2) code = "UTF-16"; else if (head[0] == -2 && head[1] == -1) code = "Unicode"; else if (head[0] == -17 && head[1] == -69 && head[2] == -65) code = "UTF-8"; inputStream.close(); return code; }

 

posted @ 2020-07-19 17:01  德华。  阅读(282)  评论(0编辑  收藏  举报