how to implement UTF-8 format in Swing application

2020-04-01 06:22发布

In my Swing chat application i am having the send button, one text area, and a text field.

If I press Send button, I need to send the text from text field to text area. It is working fine in English but not in local language.

Please give some idea or some code that will help me to solve this.

1条回答
放我归山
2楼-- · 2020-04-01 07:00

First of all, the internal character representation of String is UTF-16, so you don't need to worry once you have the string in your JVM.

The problem is probably the conversion between a sequence of characters that gets sent over the internet and a String object. When parsing a string you need to provide the encoding, e.g. when using InputStreamReader, you have to pass the Charset parameter:

InputStreamReader(InputStream in, Charset cs)
Create an InputStreamReader that uses the given charset.

The encoding has to be provided, because Java can't magically guess the encoding of a byte sequence.

查看更多
登录 后发表回答