How to ensure that Strings are in UTF-8?

前端 未结 2 1225
青春惊慌失措
青春惊慌失措 2021-02-15 14:07

How to convert this String the surveyÂ’s rules to UTF-8 in Scala?

I tried these roads but does not work:

scala> val text =         


        
相关标签:
2条回答
  • 2021-02-15 14:08

    Note that when you call text.getBytes() without arguments, you're in fact getting an array of bytes representing the string in your platform's default encoding. On Windows, for example, it could be some single-byte encoding; on Linux it can be UTF-8 already.

    To be correct you need to specify exact encoding in getBytes() method call. For Java 7 and later do this:

    import java.nio.charset.StandardCharsets
    
    val bytes = text.getBytes(StandardCharsets.UTF_8)
    

    For Java 6 do this:

    import java.nio.charset.Charset
    
    val bytes = text.getBytes(Charset.forName("UTF-8"))
    

    Then bytes will contain UTF-8-encoded text.

    0 讨论(0)
  • 2021-02-15 14:21

    Just set the JVM's file.encoding parameter to UTF-8 as follows:

    -Dfile.encoding=UTF-8
    

    It makes sure that UTF-8 is the default encoding.

    Using scala it could be scala -Dfile.encoding=UTF-8.

    0 讨论(0)
提交回复
热议问题