java中读取txt文件获得编码格式方法
private String codetype(byte[] head) { byte[] codehead = new byte[4]; // 截取数组 System.arraycopy(head, 0, codehead, 0, 4); String code = ""; 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"; else { code = "gb2312"; } return code; }
byte[] head:表示txt文件的字节数组
unicode unicode big endian
utf-8 utf-8
utf-16 unicode
gb2312 ANSI