EF BB BF - UTF-8
FF FE - UTF-16LE
FE FF - UTF-16BE
/** * 判断文件的编码格式 * @param fileName :file * @return 文件编码格式 * @throws Exception */ def codeString( fName : String ) : String = { val bin = new java.io.BufferedInputStream( new java.io.FileInputStream(fName)) val p = (bin.read() << 8) + bin.read var code = ""; p match { case 0xefbb => code = "UTF-8"; case 0xfffe => code = "Unicode"; case 0xfeff => code = "UTF-16BE"; case _ => code = "GBK"; } code }