2015-09-20 67 views
1

我有编码麻烦与俄罗斯simbols。首先,我已经决定控制台输出问题,此代码:编码控制台俄语sumbols输入

String consoleEncoding = System.getProperty("consoleEncoding"); 
if (consoleEncoding != null) { 
    try { 
     System.setOut(new PrintStream(System.out, true, consoleEncoding)); 
    } catch (java.io.UnsupportedEncodingException ex) { 
     System.err.println("Unsupported encoding set for console: "+consoleEncoding); 
    } 
} 

,但我有控制台Scanner输入,可以有俄罗斯的符号太多,但是,如果将这样的

Scanner sc = new Scanner (System.in); 
String text = sc.nextline(); //if user input here eg "Привет,Мир" 
System.out.println(text); // here I will recieve "???,??" 

扫描仪SC =新的扫描仪( System.in,“UTF-8);

或”cp866“不适用于我(或仅适用于Windows)我需要这样的通用决策,可以适应并接受输入俄语字符到任何操作系统控制台(mac OS/win)

回答

1
Locale loc = new Locale("ru"); 
Scanner sc = new Scanner(System.in, "UTF-8"); 
sc.useLocale(loc); 
+0

谢谢你的快速回答,它只适用于位于俄罗斯地区的mashines吗? – TheOriginalNickname

+0

'useLocale(Locale locale)'方法将此扫描器的语言环境设置为指定的语言环境。这是'Scanner sc',它将使用'Russian Locale'。它与位于俄罗斯地区的机器无关。 – thegauravmahawar

+0

不幸的是,它并没有帮助 – TheOriginalNickname

0

语言环境对我没有帮助。该文件应该保存为UTF-8,并且运行该程序的控制台也必须使用UTF-8。当我需要使用俄语字符时,我只需通过eclipse运行程序,这对我很有用。要保存为UTF-8,请右键单击包资源管理器 - >属性 - >文本文件编码中的文件,选择其他,然后输入UTF-8。