有没有办法为 java.lang.StringBuilder 指定字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17907727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:53:48  来源:igfitidea点击:

Is there a way to specify the character encoding to the java.lang.StringBuilder

javacharacter-encoding

提问by Mr_and_Mrs_D

Or am I stuck with :

或者我坚持:

String s = new String(new byte[0], Charset.forName("ISO-8859-1"));
// or ISO_8859_1, or LATIN-1 or ... still no constants for those
for (String string : strings) { // those are ISO-8959-1 encoded
    s += string; // hopefully this preserves the encoding (?)
}

回答by Jon Skeet

Strings are alwaysUTF-16-encoded in Java. They're justsequences of charvalues, which are UTF-16 code units. When you specify the encoding to the String(byte[], String)constructor, it's justsaying how to decode the bytes into text - the encoding is discarded afterwards.

字符串在 Java中始终采用UTF-16 编码。它们只是char值的序列,它们是 UTF-16 代码单元。当您为String(byte[], String)构造函数指定编码时,它只是说明如何将字节解码为文本 - 之后会丢弃编码。

If you need to preserve an encoding, you'll need to create your own class to keep a Charsetand Stringtogether. I can't say that I've ever wanted to do that though - are you really sure you need to?

如果您需要保留编码,则需要创建自己的类来保持一个CharsetString在一起。我不能说我曾经想这样做 - 你真的确定你需要这样做吗?

(So your "stuck with" code wouldn't work anyway - and it would also be inefficient.)

(因此,您的“卡住”代码无论如何都不起作用-而且效率也很低。)

回答by gavenkoa

How about to use converter with caching:

如何使用带缓存的转换器:

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1<<10);
    OutputStreamWriter osw = null;
    try {
        osw = new OutputStreamWriter(baos, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
    }
    osw.write("Привет!");
    osw.flush();
    System.out.println("Hello: " + baos.toString("UTF-8"));
}