Java String.getBytes( charsetName ) vs String.getBytes ( Charset object )(Java String.getBytes( charsetName ) vs String.getBytes ( Charset 对象))
问题描述
我需要使用 UTF-8 编码将字符串编码为字节数组.我正在使用谷歌番石榴,它的 Charsets 类已经为 UTF-8 编码定义了 Charset 实例.我有两种方法:
I need to encode a String to byte array using UTF-8 encoding. I am using Google guava, it has Charsets class already define Charset instance for UTF-8 encoding. I have 2 ways to do:
String.getBytes( charsetName )
String.getBytes( charsetName )
try {
byte[] bytes = my_input.getBytes ( "UTF-8" );
} catch ( UnsupportedEncodingException ex) {
}
String.getBytes(字符集对象)
String.getBytes( Charset object )
// Charsets.UTF_8 is an instance of Charset
byte[] bytes = my_input.getBytes ( Charsets.UTF_8 );
我的问题是我应该使用哪一种?它们返回相同的结果.对于方式 2 - 我不必放置 try/catch!我查看了 Java 源代码,发现方式 1 和方式 2 的实现方式不同.
My question is which one I should use? They return the same result. For way 2 - I don't have to put try/catch! I take a look at the Java source code and I see that way 1 and way 2 are implemented differently.
有人有什么想法吗?
推荐答案
如果您打算使用字符串字面量(例如UTF-8")……您不应该使用.而是使用第二个版本并提供来自 的常量值StandardCharsets
(特别是在本例中为 StandardCharsets.UTF_8
).
If you are going to use a string literal (e.g. "UTF-8") ... you shouldn't. Instead use the second version and supply the constant value from StandardCharsets
(specifically, StandardCharsets.UTF_8
, in this case).
当字符集是动态时使用第一个版本.当您在编译时不知道字符集是什么时,就会出现这种情况;它由最终用户提供,从配置文件或系统属性等中读取.
The first version is used when the charset is dynamic. This is going to be the case when you don't know what the charset is at compile time; it's being supplied by an end user, read from a config file or system property, etc.
在内部,这两种方法都调用了一个版本的 StringCoding.encode()
.encode()
的第一个版本只是首先通过提供的名称查找 Charset
,如果该字符集未知/不可用则抛出异常.
Internally, both methods are calling a version of StringCoding.encode()
. The first version of encode()
is simply looking up the Charset
by the supplied name first, and throwing an exception if that charset is unknown / not available.
这篇关于Java String.getBytes( charsetName ) vs String.getBytes ( Charset 对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java String.getBytes( charsetName ) vs String.getBytes ( Char


基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01