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

查看:178
本文介绍了Java String.getBytes(charsetName)vs String.getBytes(Charset object)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用UTF-8编码将字符串编码为字节数组。我使用谷歌番石榴,它有Charsets类已经定义的Charset实例为UTF-8编码。我有2种方法:

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(Charset object)

  • 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 ,并抛出异常if该字符集是未知/不可用。

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

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