在 Java 中读/写时如何强制使用 UTF-16?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098186/
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
How to force UTF-16 while reading/writing in Java?
提问by IAmYourFaja
I see that you can specify UTF-16 as the charset via Charset.forName("UTF-16")
, and that you can create a new UTF-16 decoder via Charset.forName("UTF-16").newDecoder()
, but I only see the ability to specify a CharsetDecoder
on InputStreamReader
's constructor.
我看到您可以通过 将 UTF-16 指定为字符集Charset.forName("UTF-16")
,并且您可以通过来创建新的 UTF-16 解码器Charset.forName("UTF-16").newDecoder()
,但我只看到能够指定CharsetDecoder
onInputStreamReader
的构造函数。
How so how do you specify to use UTF-16 while reading any stream in Java?
那么如何在 Java 中读取任何流时指定使用 UTF-16 呢?
回答by Isaac
Input streams deal with raw bytes. When you read directly from an input stream, all you get is raw bytes where character sets are irrelevant.
输入流处理原始字节。当您直接从输入流中读取时,您得到的只是原始字节,其中字符集无关紧要。
The interpretation of raw bytes into characters, by definition, requires some sort of translation: how do I translate from raw bytes into a readable string? That "translation" comes in the form of a character set.
根据定义,将原始字节解释为字符需要某种转换:如何将原始字节转换为可读字符串?该“翻译”以字符集的形式出现。
This "added" layer is implemented by Readers. Therefore, to read characters (rather than bytes) from a stream, you need to construct a Reader
of some sort (depending on your needs) on top of the stream. For example:
这个“添加”层是由 Readers 实现的。因此,要从流中读取字符(而不是字节),您需要Reader
在流的顶部构造某种类型(取决于您的需要)。例如:
InputStream is = ...;
Reader reader = new InputStreamReader(is, Charset.forName("UTF-16"));
This will cause reader.read()
to read characters using the character set you specified. If you would like to read entire lines, use BufferedReader
on top:
这将导致reader.read()
使用您指定的字符集读取字符。如果您想阅读整行,请BufferedReader
在顶部使用:
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-16")));
String line = reader.readLine();