'Is the encoding name UTF8 or UTF-8?
Some httpclient requires me to give a string charsetµ.
I am wondering whether to give it UTF8 or utf8 or UTF-8?
Especially when calling Charset.forName(enc)
Solution 1:[1]
Easier to use StandardCharsets.UTF_8 as it saves you having to deal with UnsupportedEncodingExceptions
Solution 2:[2]
UTF-8 is the standard one and most people use it.
Here is the list of charsets used in Java:
http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
N.B.
If charset factory/singleton classes can take Enum as parameter, as well as String, Enum is, IMO, always a safer and better option.
Solution 3:[3]
FYI, For Java 7/tomcat 8.5 I am using: -Dfile.encoding=UTF-8 -Dclient.encoding.override=UTF-8
But for Java 8/tomcat 8.5 I got: java.nio.charset.IllegalCharsetNameException: UTF-8
So I changed to UTF8
Solution 4:[4]
The standard name is 'UTF-8'. Source code(I use jdk1.8.0_20) showes more detail:
UTF_8 extends Unicode {
public UTF_8() {
super("UTF-8", StandardCharsets.aliases_UTF_8); // show other aliases
// static final String[] aliases_UTF_8 = new String[]{"UTF8", "unicode-1-1-utf-8"};
}
public String historicalName() {
// return old name of UTF-8
return "UTF8";
}
Solution 5:[5]
Charset charset = Charset.forName("cp1254");
This was the utf-8 solution for me. You can use it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | eleven |
| Solution 3 | Bob Hastings |
| Solution 4 | ridox |
| Solution 5 | Yaser |
