Friday, October 15, 2010

CSV Encoding

Two relatively simple lines of code:

FileInputStream fstream = new FileInputStream("C:\\MyFilePath\\" + "myFileName.csv");

BufferedReader reader = new BufferedReader(new InputStreamReader(
fstream, "UTF-8"));

When I run this code and try to do a reader.readLine() call I wasn't seeing the line in the csv file coming back properly.

I changed UTF-8 to UTF-16 as the encoding of the InputStreamReader and was having a little bit more success, but still odd characters were showing up in between legit characters.

I did some reading and I'm not sure if this is all CSV files but there is an encoding the InputStreamReader accepts called UTF-16LE (LE for Little Endian). Changing that as the encoding type to my InputStreamReader did the trick and I can now read line by line successfully.

No comments:

Post a Comment