I'm trying to use a constant instead of a string literal in this piece of code:
new InputStreamReader(new FileInputStream(file), "UTF-8")
"UTF-8"
appears in the code rather often, and would be much better to refer to some static final
variable instead. Do you know where I can find such a variable in JDK?
BTW, on a second thought, such constants are bad design: Public Static Literals ... Are Not a Solution for Data Duplication
A Java String is internally always encoded in UTF-16 - but you really should think about it like this: an encoding is a way to translate between Strings and bytes.
Introduction. When working with Strings in Java, we oftentimes need to encode them to a specific charset, such as UTF-8. UTF-8 represents a variable-width character encoding that uses between one and four eight-bit bytes to represent all valid Unicode code points.
UTF-8 is a Unicode character encoding method. This means that UTF-8 takes the code point for a given Unicode character and translates it into a string of binary. It also does the reverse, reading in binary digits and converting them back to characters.
In Java 1.7+, java.nio.charset.StandardCharsets defines constants for Charset
including UTF_8
.
import java.nio.charset.StandardCharsets; ... StandardCharsets.UTF_8.name();
For Android: minSdk 19
Now I use org.apache.commons.lang3.CharEncoding.UTF_8
constant from commons-lang.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With