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

Java

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

The Google Guava library (which I'd highly recommend anyway, if you're doing work in Java) has a Charsets class with static fields like Charsets.UTF_8, Charsets.UTF_16, etc.

Since Java 7 you should just use java.nio.charset.StandardCharsets instead for comparable constants.

Note that these constants aren't strings, they're actual Charset instances. All standard APIs that take a charset name also have an overload that take a Charset object which you should use instead.

Solution 4 - Java

In case this page comes up in someones web search, as of Java 1.7 you can now use java.nio.charset.StandardCharsets to get access to constant definitions of standard charsets.

Solution 5 - Java

This constant is available (among others as: UTF-16, US-ASCII, etc.) in the class org.apache.commons.codec.CharEncoding as well.

Solution 6 - Java

There are none (at least in the standard Java library). Character sets vary from platform to platform so there isn't a standard list of them in Java.

There are some 3rd party libraries which contain these constants though. One of these is Guava (Google core libraries): http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Charsets.html

Solution 7 - Java

You can use Charset.defaultCharset() API or file.encoding property.

But if you want your own constant, you'll need to define it yourself.

Solution 8 - Java

In Java 1.7+

Do not use "UTF-8" string, instead use Charset type parameter:

import java.nio.charset.StandardCharsets

...

new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);

Solution 9 - Java

If you are using OkHttp for Java/Android you can use the following constant:

import com.squareup.okhttp.internal.Util;

Util.UTF_8; // Charset
Util.UTF_8.name(); // String

Solution 10 - Java

Constant definitions for the standard. These charsets are guaranteed to be available on every implementation of the Java platform. since 1.7

 package java.nio.charset;
 Charset utf8 = StandardCharsets.UTF_8;

Solution 11 - Java

Class org.apache.commons.lang3.CharEncoding.UTF_8 is deprecated after Java 7 introduced java.nio.charset.StandardCharsets

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyegor256View Question on Stackoverflow
Solution 1 - JavaRogerView Answer on Stackoverflow
Solution 2 - Javayegor256View Answer on Stackoverflow
Solution 3 - JavaDaniel PrydenView Answer on Stackoverflow
Solution 4 - JavacosjavView Answer on Stackoverflow
Solution 5 - JavaAlfredo CarrilloView Answer on Stackoverflow
Solution 6 - JavatskuzzyView Answer on Stackoverflow
Solution 7 - Javapaulsm4View Answer on Stackoverflow
Solution 8 - JavaMostafa VatanpourView Answer on Stackoverflow
Solution 9 - JavaJJDView Answer on Stackoverflow
Solution 10 - JavaVazgen TorosyanView Answer on Stackoverflow
Solution 11 - Javasendon1982View Answer on Stackoverflow