Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading CSV file in java adds space between each character

I'm reading a CSV file downloaded form google trend, here are the contents of file when opened in notepad (first two lines only):

ferrari ferrari (std error)
0.735 2%

When I read the file using readline the line read contains space between each character, in above case the output is:

f e r r a r i f e r r a r i ( s t d e r r o r )
0 . 7 3 5 2 %

(There are tabs between "ferrari" and "ferrari" and between 0.735 and 2% which stackoverflow is not showing)

Newline character at end of each line is also read two times. Why is it that? Any solution?

Here is the code I'm using to read the file:

BufferedReader Reader = new BufferedReader(new FileReader("trend.csv"));
String line = null;
while ((line = Reader.readLine()) != null)
    System.out.println(line);

Edit: there are also some strange characters read at the starting of file

Edut: Got the solution

It was the encoding problem, changed the first line to:

BufferedReader Reader = new BufferedReader(new InputStreamReader(new FileInputStream("trend.csv"), "UTF-16"));
like image 721
Uzair Farooq Avatar asked Jan 05 '12 04:01

Uzair Farooq


Video Answer


1 Answers

It is due to the character encoding... I've just downloaded the file from trends and tried, it had the same problem.

I got around with this if I use UTF-16 character set.

public class TrendReader
{
    public static void main(String args[]) throws Exception
    {
        //BufferedReader Reader = new BufferedReader(new FileReader("trends.csv"));
        BufferedReader Reader = new BufferedReader(new InputStreamReader(new FileInputStream("trends.csv"), "UTF-16"));
        String line = null;
        while ((line = Reader.readLine()) != null)
        {
            System.out.println(line);
        }
    }
}
like image 159
Santhosh Reddy Mandadi Avatar answered Nov 15 '22 18:11

Santhosh Reddy Mandadi