Home  >  Q&A  >  body text

如图,java中同样是utf-8存储的字符串,为什么这两种方式输出的字节会不同?

PHP中文网PHP中文网2741 days ago528

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:50:35

    First of all, make sure the encoding is unified and the file is encodedUTF-8,以UTF-8去读文件,getBytes也传入UTF-8
    Also, don’t use Notepad! Don't use Notepad! Don't use notepad! Say important things three times! ! !

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:50:35

    It is said in the Java8 documentation that String.getBytes() is encoded according to the platform's default character set. In case of Windows, the default character set is not utf-8, but gbk. Linux depends on the configuration (I don’t know exactly how).

    Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

    The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

    Portal: String.getBytes()

    reply
    0
  • Cancelreply