Link to home
Avatar of Ted Penner
Ted PennerFlag for United States of America

asked on

Unable to read CSV file with this Java Code

We are trying to read this CSV file line by line using the below code but a row in the CSV file seems to stop the execution of the program in the middle. If we delete that row manually the program is able to read all the rows till the end.

Read is successful until ID 32267. After that, the program appears to be stopped. If we delete the next row ID 32268, manually from the CSV file, then the program is able to read the complete file without any error.

Java Code -

CSVReader reader = new CSVReader(new FileReader(fileName));
String[] row;

while ((row = reader.readNext()) != null) {
                System.out.println(Arrays.toString(row));
}
reader.close();

Open in new window

Data - Copy.zip
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That code you posted couldn't be problematic in the way you describe, so it's probably not the real code…

UPDATE:
Hmm. Is this Opencsv?
Avatar of Ted Penner

ASKER

If you download and unzip the attached CSV, you should be able to run that code against it. We use IntelliJ to run it.
Yes, it is OpenCSV
I see the problem, though not the reason for it. Let me ponder…
I suggest, you take another approach ;-)
Depending on your Java version, you may try this one:
https://dzone.com/articles/how-to-read-a-big-csv-file-with-java-8-and-stream
OpenCSV is able to read complete file if we just delete a single row from it (Id - 32268) and deleting a single row doesn't make much difference to the size of the file. So the size of the file doesn't seem to be an issue here
A possible alternative:
    public static void test1() throws Exception
    {
        try (FileInputStream inputStream = new FileInputStream("C:\\temp\\Data - Copy.csv");
                Scanner sc = new Scanner(inputStream))
        {
            while (sc.hasNextLine())
            {
                String line = sc.nextLine();
                System.out.println(line);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

Open in new window

For parsing, this one:
    public static void test1() throws Exception
    {
        try (FileInputStream inputStream = new FileInputStream("C:\\temp\\Data - Copy.csv");
                Scanner sc = new Scanner(inputStream))
        {
            while (sc.hasNextLine())
            {
                String line = sc.nextLine();
                String[] strArray = line.split(",");
                for (String str : strArray)
                {
                    System.out.println(str);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

Open in new window

Gotcha ;-)
It's because of the backslash here:
"MISSING CATALYTIC CONVERTER\"
Hi Alex, Thank you. The backslash does seem like the problem. How might we overcome it?
ASKER CERTIFIED SOLUTION
Avatar of Alex [***Alex140181***]
Alex [***Alex140181***]
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial