A
A
Arthur2016-03-19 22:33:29
Java
Arthur, 2016-03-19 22:33:29

How to correctly display Russian words from the CMD.exe console using JAVA?

Greetings!
Friends, there is a simple (as I thought) task.
The java application is launched in the Windows console.
The application asks you to enter a string, after which it displays it next.
Everything would be fine, but I ran into a problem.
When I write a word in Cyrillic, it does not read the letter "SH" (large). It displays "?" instead.
I looked through the Internet, wherever I found, they write to replace the encoding on the input stream and on the output. I tried this and that, and cp866 and cp1251 encoding.
I achieved that the Cyrillic alphabet is displayed normally, everything except the letter Ш.
And, interestingly, the given line from the program displays correctly.
Friends, please help!
Or tell me how to fix it, or where to dig.
Program output


E:\work\java\SimpleComandLineApp\out\production\SimpleComandLineApp>java en.antonov.Main
Hello! What do you want me to display on the console screen?
an english word
I display on the screen (how will I show the letter Ш?) >an english word
Do you want to write something else?
The word SHKVARKI, - said the lisping snake and crawled into the rough bushes
, I display it on the screen (how will I show the letter Ш?
)
I display the SHQUARKS
on the screen (how will I show the letter Ш?) >? QUARKS
Do you want to write something else?
exit
Bye! SHSHSHSHSH

Below is the program code
public class Main {

    public static void main(String[] args) throws Exception {
  // write your code here
        System.out.println("Привет! Что ты хочешь, чтобы я вывел на экран консоли?");

        //подмена стандартного вывода другим потоком, который выводит в кодировке Cp866
        PrintStream systemOut = System.out;
        PrintStream printStream = new PrintStream(System.out, true, "Cp866");
        System.setOut(printStream);

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String userText;
        String data = reader.readLine();
        while (!"exit".equals(data)) {
            userText = data;
            //вывод в консоль слова в кодировке Cp866
            String text = "вывожу на экран (а букву Ш покажу как ?) >";
            System.out.print(text);
            System.out.write(userText.getBytes());

            System.out.println("\nЧто-то еще хочеШь написать? <or exit>");
            data = reader.readLine();
        }
        printStream.close();
        reader.close();
        System.setOut(systemOut);
        System.out.println("Пока! ШШШШШ");
    }
}

UPD:
Source file encoding UTF-8

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question