簡體   English   中英

Java字符串 - UTF和字節表示

[英]Java string - UTF and byte representation

我想知道它的java字符串和字節表示。 我有一個用UTF-16小端編碼的文件,當我在我的hexeditor中查看時,我可以看到

ff fe 61 00 f3 00 61 00 00

現在,當我使用它加載到Java時

 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-16"));
    StringBuilder builder = new StringBuilder();
    String line;

    while ((line = reader.readLine()) != null)
        builder.append(line);
    System.out.println(Arrays.toString(builder.toString().getBytes()));

我可以在輸出中看到

[97, -13, 97]

如果我打印字節為什么我不能看到我可以在我的hexeditor中看到的零值?

這是因為Java不會將UTF-16格式的字符串保留在內存中,這會浪費,並且因為getBytes返回默認系統字符集中的字符串(在您的機器上可能不是UTF-16) javadoc 正確的重載是getBytes(“UTF-16”) - 這樣你應該在結尾處看到0填充,並且可能在開頭看到BOM(ff fe)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM