使用java判断TXT文件的编码格式

来源:互联网 发布:js onclick事件传参 编辑:程序博客网 时间:2024/06/06 12:24

不和你多BB,直接上代码。


private static String getCharset(String fileName) throws IOException{                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));          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";          }          return code;}


0 0
原创粉丝点击