Java InputStream encoding/charset

前端 未结 2 1297
刺人心
刺人心 2020-12-16 15:26

Running the following (example) code

import java.io.*;

public class test {
    public static void main(String[] args) throws Exception {
        byte[] buf          


        
2条回答
  •  [愿得一人]
    2020-12-16 15:39

    As noted, getBytes() (no-arguments) uses the Java platform default encoding, which may not be ISO-8859-1. Simply printing it should work, provided your terminal and the default encoding match and support the character. For instance, on my system, the terminal and default Java encoding are both UTF-8. The fact that you're seeing a '?' indicates that yours don't match or å is not supported.

    If you want to manually encode to UTF-8 on your system, do:

    String s = r.readLine();
    byte[] utf8Bytes = s.getBytes("UTF-8");
    

    It should give a byte array with {-61, -91}.

提交回复
热议问题