BufferedReader returns ISO-8859-15 String - how to

2020-03-18 06:57发布

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.

4条回答
冷血范
2楼-- · 2020-03-18 07:28

Try this:

BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                            ftp.getInputStream(),
                            Charset.forName("ISO-8859-15")
                        )
                    );
String row = br.readLine();
查看更多
来,给爷笑一个
3楼-- · 2020-03-18 07:29

If the file/stream/whatever really contains ISO-8859-15 encoded text, you just need to specify that when you create the 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).

查看更多
The star\"
4楼-- · 2020-03-18 07:29

Have you tried:

BufferedReader r = new BufferedReader(new InputStreamReader("ISO-8859-1"))
...
查看更多
再贱就再见
5楼-- · 2020-03-18 07:32

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.

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

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.

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

查看更多
登录 后发表回答