Java BufferedReader 返回 ISO-8859-15 字符串 - 如何转换为 UTF16 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1069922/
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-08-11 23:05:24  来源:igfitidea点击:

BufferedReader returns ISO-8859-15 String - how to convert to UTF16 String?

javacharacter-encoding

提问by tputkonen

I have an FTP client class which returns InputStream pointing the file. I would like to read the file row by row with BufferedReader. The issue is, that the client returns the file in binary mode, and the file has ISO-8859-15 encoding.

我有一个 FTP 客户端类,它返回指向文件的 InputStream。我想用 BufferedReader 逐行读取文件。问题是,客户端以二进制模式返回文件,并且该文件具有 ISO-8859-15 编码。

采纳答案by Alan Moore

If the file/stream/whatever really contains ISO-8859-15 encoded text, you just need to specify that when you create the InputStreamReader:

如果文件/流/任何内容确实包含 ISO-8859-15 编码文本,则只需在创建 InputStreamReader 时指定:

BufferedReader br = new BufferedReader(
    new InputStreamReader(ftp.getInputStream(), "ISO-8859-15"));

Then readLine()will create valid Strings in Java's native encoding (which is UTF-16, not UTF-8).

然后readLine()将以 Java 的本机编码(UTF-16,而不是 UTF-8)创建有效的字符串。

回答by Brian Agnew

The original string is in ISO-8859-15, so the byte stream read by your InputStreamReader will be in this encoding. So read in using that encoding (specify this in the InputStreamReader constructor). That tells the InputStreamReader that the incoming byte stream is in ISO-8859-15 and to perform the appropriate byte-to-character conversions.

原始字符串在 ISO-8859-15 中,因此 InputStreamReader 读取的字节流将采用此编码。所以使用该编码读入(在 InputStreamReader 构造函数中指定)。这告诉 InputStreamReader 传入的字节流在 ISO-8859-15 中并执行适当的字节到字符转换。

Now it will be in the standard Java UTF-16 format, and you can then do what you wish.

现在它将采用标准的 Java UTF-16 格式,然后您就可以随心所欲了。

I think the current problem is that you're reading it using your default encoding (by not specifying an encoding in InputStreamReader), and then trying to convert it, by which time it's too late.

我认为当前的问题是您正在使用默认编码(通过未在 InputStreamReader 中指定编码)读取它,然后尝试转换它,到那时为时已晚。

Using default behaviour for these sort of classes often ends in grief. It's a good idea to specify encodings wherever you can, and/or default the VM encoding via -Dfile.encoding

对这些类使用默认行为通常会以悲痛告终。最好在任何可能的地方指定编码,和/或通过以下方式默认 VM 编码-Dfile.encoding

回答by Brian Clapper

Have you tried:

你有没有尝试过:

BufferedReader r = new BufferedReader(new InputStreamReader("ISO-8859-1"))
...

回答by bruno conde

Try this:

尝试这个:

BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                            ftp.getInputStream(),
                            Charset.forName("ISO-8859-15")
                        )
                    );
String row = br.readLine();