android学习:自动识别文本文件编码格式
/** * 判断文件的编码格式 * @param fileName :file * @return 文件编码格式 * @throws Exception */ public static String codeString(String fileName) throws Exception{ 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; }
有了这个函数,妈妈再也不用担心我的文本文件打开是乱码了,O(∩_∩)O哈哈~