I want to get the text from the txt file and print in terminal.The Russian letters won't display correctly.

I did the next things to fix the problem

  1. https://stackoverflow.com/a/34717160/14048134

  2. https://stackoverflow.com/a/36932975/14048134

  3. https://stackoverflow.com/a/61205022/14048134

  4. go to Help | Edit Custom VM Options... and add (restart IDE isneeded):-Dconsole.encoding=UTF-8-Dfile.encoding=UTF-8

Here's my code:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));try {StringBuilder sb = new StringBuilder();String line = br.readLine();while (line != null) {sb.append(line);sb.append(System.lineSeparator());line = br.readLine();}String everything = sb.toString();System.out.println(new String(everything.getBytes(),StandardCharsets.UTF_8));} catch (IOException e) {throw new RuntimeException(e);} finally {br.close();}

No matter what I do, the Russian letter's won't be displayed.

enter image description here

enter image description here

enter image description here

enter image description here

After all, no Russian letters are displayed correctly, but the Latin ones and numbers are correct.

How can I fix this problem? Any ideas?

UPDATE 1

 BufferedReader br = new BufferedReader(new FileReader("file.txt",StandardCharsets.UTF_8));try {StringBuilder sb = new StringBuilder();String line = br.readLine();while (line != null) {sb.append(line);sb.append(System.lineSeparator());line = br.readLine();}String everything = sb.toString();System.out.println(everything);} catch (IOException e) {throw new RuntimeException(e);} finally {br.close();}

This one doesn't work too.

UPDATE 2

Here's the console settings.

enter image description here

There is no benefit from this.

UPDATE 3

Expected result (the original text): "Кстати, в Индонезии есть часть страны называемой Java...Карл, Java!!! Еще и страннее то, что как бы Индонезия кажется такимродным.... с чего это вдруг?"

Actual result (what is displayed in console): "Кстати, в �?ндонезии есть часть страны называемой Java...Карл, Java!!! Еще и страннее то, что как бы �?ндонезия кажется такимродным.... с чего это вдруг?"

1

Best Answer


First try to use method getBytes(Charset charset) instead of just getBytes() in your line:

System.out.println(new String(everything.getBytes(),StandardCharsets.UTF_8));

This may solve your problem. Also, for diagnostics I would suggest to try convert your string that you think contains russian characters into unicode sequence and see if your string really contains russian characters and your problem is only display problem or that your String doesn't have the right chars to begin with. I wrote my own feature that can easily convert string into unicode sequence and vice-versa. Here how it works:There is an Open Source java library MgntUtils written and maintained by me that has a Utility that converts Strings to unicode sequence and vise versa:

result = "Hello World";result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);System.out.println(result);result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);System.out.println(result);

The output of this code is:

\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064Hello World

The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder