Getting unicode values from System.in

别来无恙 提交于 2019-12-10 14:45:54

问题


I have created a Scanner that gets input from System.in so that I can get input from the console.

Scanner scanner = new Scanner(System.in, "UTF-8");

When I do

String s = scanner.next();

and then input Слово דבר in the console, the value of the string becomes ???? ???.

The console is able to display Unicode characters, but why can't I read them?


回答1:


It's not safe to assume System.in is UTF-8 encoded. See this question for some workarounds.




回答2:


This is because System.in returns text in default encoding (your default encoding is evidently not UTF-8). This should work OK

Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(s);

And you can read your default encoding from Java

System.out.println(System.getProperty("file.encoding"));


来源:https://stackoverflow.com/questions/16050123/getting-unicode-values-from-system-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!