从哪里获得“UTF-8” Java中的字符串文字? [英] Where to get "UTF-8" string literal in Java?

查看:1631
本文介绍了从哪里获得“UTF-8” 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代码中经常出现,而且更好地引用一些静态最终变量。你知道我在JDK中哪里可以找到这样一个变量吗?

"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,再想一想,这样的常量设计不好:公共静态文字...不是数据复制的解决方案

BTW, on a second thought, such constants are bad design: Public Static Literals ... Are Not a Solution for Data Duplication

推荐答案

在Java 1.7+中, java.nio.charset.StandardCharsets 定义 Charset 的常量,包括 UTF_8

In Java 1.7+, java.nio.charset.StandardCharsets defines constants for Charset including UTF_8.

import java.nio.charset.StandardCharsets;

...

StandardCharsets.UTF_8.name();

适用于Android:minSdk 19

这篇关于从哪里获得“UTF-8” Java中的字符串文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆