Java String.getBytes( charsetName ) vs String.getBytes ( Charset 对象) [英] Java String.getBytes( charsetName ) vs String.getBytes ( Charset object )

查看:45
本文介绍了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:

  1. String.getBytes( charsetName )

  1. 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 对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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