首頁  >  問答  >  主體

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

PHP中文网PHP中文网2741 天前527

全部回覆(2)我來回復

  • 大家讲道理

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

    先確保編碼統一,文件編碼UTF-8,以UTF-8去读文件,getBytes也传入UTF-8
    另,不要用記事本!不要用記事本!不要用記事本!重要的事情說三遍! ! !

    回覆
    0
  • 迷茫

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

    在 Java8 的文檔中說 String.getBytes() 是按平台預設的字元集來編碼。如果是 Windows,預設字元集不是 utf-8,而是 gbk。 Linux 要看配置(具體如何我不是很清楚)。

    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 require be used when more control over the encoding process is required.

    傳送門:String.getBytes()

    回覆
    0
  • 取消回覆