Ted Penner
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.
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();
Data - Copy.zip
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.
ASKER
Yes, it is OpenCSV
I see the problem, though not the reason for it. Let me ponder…
Seems as if OpenCSV can't handle big files:
https://stackoverflow.com/questions/14988577/opencsv-not-reading-my-entire-file
https://stackoverflow.com/questions/14988577/opencsv-not-reading-my-entire-file
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
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
ASKER
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();
}
}
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();
}
}
Gotcha ;-)
It's because of the backslash here:
"MISSING CATALYTIC CONVERTER\"
It's because of the backslash here:
"MISSING CATALYTIC CONVERTER\"
ASKER
Hi Alex, Thank you. The backslash does seem like the problem. How might we overcome it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
UPDATE:
Hmm. Is this Opencsv?