2

I'm trying to print Russian text like the below example but got ? characters. I have tried multiple "encodings" but the same result.

public static void main(String[] args) throws Exception {
        String t = "тест";
        System.out.println("test: " + t);
    }

Output: test: ????

How can I do right encoding?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Hrant Vardanyan
  • 265
  • 1
  • 4
  • 11

1 Answers1

3

The characters that you see couldn't be decoded properly on the device displaying them. It could be many reasons, but the main cause is the device is using another encoding scheme. To properly encode character stream use the following code that writes to the file.

public static void main(String[] args) throws Exception {
    OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream("myFile.txt"), "windows-1251");

    String t = "тест";
    fileWriter.write("test: " + t);
}
Roman C
  • 49,761
  • 33
  • 66
  • 176