判断文件的编码格式

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 
  }

 

posted @ 2016-07-17 20:43  澄轶  阅读(650)  评论(0编辑  收藏  举报