使用sparkscala获取字符编码信息

huwehgph  于 2021-07-12  发布在  Spark
关注(0)|答案(0)|浏览(222)

//用于检查文件编解码器的函数

import scala.io.Source

def checkCodec(filename:String): String = {
val bufferedSource = Source.fromFile(filename)
val codec:String = (bufferedSource.codec).toString
println("bufferedSource.codec - " +bufferedSource.codec)
bufferedSource.close
if(codec.equalsIgnoreCase("UTF-8")){
  return filename + " " + codec
}
else{
  return "CodecErrorDetected"
}
  }

val validFile = checkCodec(fileName)

println("The file is - "+validFile)

此函数运行良好,即使文件类型为.zip、文件格式不正确或某些损坏的文件(已使用)也会产生“utf-8”结果https://pinetools.com/corrupt-file-generator). 我怎样才能至少分辨出损坏的文件(例如:我把一个pdf文件改成了.pddssee格式,它甚至不存在,仍然被识别为utf-8文件)。需要帮助了解如何使用scala区分损坏的文件。这是我检查损坏文件的正确方法吗?
感谢您的宝贵意见。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题