获取文件编码格式

public String getCharset(String file) throws IOException {
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
        int p = (bin.read() << 8) + bin.read();
        String code = null;
        switch (p) {
        case 0xefbb:
            code = "UTF-8";
            break;
        case 0xfffe:
            code = "Unicode";
            break;
        case 0xfeff:
            code = "UTF-16BE";
            break;
        default:
            code = "GBK";
        }
        bin.close();
        return code;

    }

 

posted @ 2013-08-28 21:13  eyotata  阅读(413)  评论(0编辑  收藏  举报