在Java中读/写时如何强制UTF-16? [英] How to force UTF-16 while reading/writing in Java?

查看:189
本文介绍了在Java中读/写时如何强制UTF-16?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到您可以通过 Charset.forName(UTF-16)指定UTF-16作为字符集,并且您可以创建一个新的UTF-16解码器通过 Charset.forName(UTF-16)。newDecoder(),但我只看到能够指定 CharsetDecoder InputStreamReader 的构造函数。

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- Java中的任何流?

How so how do you specify to use UTF-16 while reading any stream in Java?

推荐答案

输入流处理原始字节。当你从输入流中直接读取时,你所得到的是字符集不相关的原始字节。

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.

这个添加层是由读者实现的。因此,要从流读取字符(而不是字节),您需要在流之上构造一些 Reader (根据您的需要)。例如:

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:

InputStream is = ...;
Reader reader = new InputStreamReader(is, Charset.forName("UTF-16"));

这将导致 reader.read()使用您指定的字符集读取字符。如果您想阅读整行,请在顶部使用 BufferedReader

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:

BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-16")));
String line = reader.readLine();

这篇关于在Java中读/写时如何强制UTF-16?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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