views:

2240

answers:

5

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.

+2  A: 

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

Brian Agnew
A: 

Have you tried:

BufferedReader r = new BufferedReader(new InputStreamReader("ISO-8859-1"))
...
Brian Clapper
s/b ISO-8859-15, not ISO-8859-1
lavinio
+6  A: 

Try this:

BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                            ftp.getInputStream(),
                            Charset.forName("ISO-8859-15")
                        )
                    );
String row = br.readLine();
bruno conde
+8  A: 

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

Alan Moore
A: 

I have a similar problem. I did this BufferedReader r = new BufferedReader(new InputStreamReader("ISO-8859-1"))

and readline gives me a String. If I write that string using

buffWrite = new BufferedWriter(new OutputStreamWriter(outputStream,"ISO-8859-1")); buffWrite.write(line);

it works perfect. However I want to save the process each line and save the data into database. When I do that, I get garbage. Is there any way to change this?

GreenT