简体   繁体   中英

Java - Russian Encoding

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM