简体   繁体   中英

Encoding string doesn't work properly in java

I am developing a JavaFX application. I need to create a TreeView programmatically using Persian language as it's nodes' name.
The problem is I see strange characters when I run the application. I have searched through the web and SO same questions. I code a function to do the encoding based on the answers to same question:

public static String getUTF(String encodeString) {
        return new String(encodeString.getBytes(StandardCharsets.ISO_8859_1),
                         StandardCharsets.UTF_8);
}

And I use it to convert my string to build the TreeView :

CheckBoxTreeItem<String> userManagement = 
             new CheckBoxTreeItem<>(GlobalItems.getUTF("کاربران"));

This answer dowsn't work properly for some characters:

在此处输入图片说明

I still get strange results. If I don't use encoding, I get:

在此处输入图片说明

For hard coded string literals you need to tell the javac compiler to use the same encoding as the java source, say UTF-8 . Check the IDE / build settings. You can u-escape some Farsi symbols, for Dal, د . If the escaped characters come thru correctly, the compiler uses the wrong encoding.

String will always contain Unicode, no new String s with hacking reconversion needed.

Reading files with text, one needs to convert those bytes (byte/InputStream) to java text (String/Reader) specifying the encoding of those bytes.

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