How to force UTF-16 while reading/writing in Java?

五迷三道 提交于 2019-12-03 20:22:49

问题


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.

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


回答1:


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:

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:

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


来源:https://stackoverflow.com/questions/15098186/how-to-force-utf-16-while-reading-writing-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!