Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to get "UTF-8" string literal in Java?

Tags:

java

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

like image 280
yegor256 Avatar asked Jul 14 '11 18:07

yegor256


People also ask

Is Java a UTF-8 String?

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.

What is StandardCharsets UTF_8 in Java?

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.

What String is UTF-8?

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.


2 Answers

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

like image 128
Roger Avatar answered Dec 14 '22 14:12

Roger


Now I use org.apache.commons.lang3.CharEncoding.UTF_8 constant from commons-lang.

like image 30
yegor256 Avatar answered Dec 14 '22 16:12

yegor256