Why do I get a MalformedInputException from this code?

前端 未结 2 1230
你的背包
你的背包 2020-12-15 17:58

I\'m a newbie in Scala, and I wanted to write some sourcecodes from myself for me to get better. I\'ve written a simple object (with a main entry) in order to simulate a \"g

相关标签:
2条回答
  • From the JavaDoc:

    MalformedInputException thrown when an input byte sequence is not legal for given charset, or an input character sequence is not a legal sixteen-bit Unicode sequence.

    Pass the currect encoding as parameter to Source.fromFile method.

    0 讨论(0)
  • 2020-12-15 18:33

    You can handle this character encoding exception by adding below snippet in your code

    import scala.io.Codec
    import java.nio.charset.CodingErrorAction
    
    
    implicit val codec = Codec("UTF-8")
    codec.onMalformedInput(CodingErrorAction.REPLACE)
    codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
    
    0 讨论(0)
提交回复
热议问题