现在的位置: 首页 > 综合 > 正文

java中读取txt文件获得编码格式方法

2013年10月03日 ⁄ 综合 ⁄ 共 412字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.