Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 462
tputkonen Avatar asked Jul 01 '09 15:07

tputkonen


Video Answer


3 Answers

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).

like image 142
Alan Moore Avatar answered Nov 13 '22 01:11

Alan Moore


Try this:

BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                            ftp.getInputStream(),
                            Charset.forName("ISO-8859-15")
                        )
                    );
String row = br.readLine();
like image 24
bruno conde Avatar answered Nov 13 '22 00:11

bruno conde


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

like image 33
Brian Agnew Avatar answered Nov 13 '22 01:11

Brian Agnew