0

I was trying a CSV using "," as the split delimiter but when I checked the file, I found that the CSV contains , in the field example

"id26305","This process, however, afforded me no means of ascertaining the dimensions of my dungeon; as I might make its circuit, and return to the point whence I set out, without being aware of the fact; so perfectly uniform seemed the wall.","EAP"

I am using Java and here's my code to read the file. Any Suggestions?

private static ArrayList<Record> readRecordFromCSVFile(String filename){
    File f=new File(filename);
    ArrayList<Record>r=new ArrayList<>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line=br.readLine();
        String line2="";

        while ((line2 = br.readLine()) != null) {
            String[]attributes=line2.split(";?(?:(?:\"((?:[^\"]|\"\")*)\")|([^;]*))");
            Record record=createRecord(attributes);
            r.add(record);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return r;
}
  • 2
    Use a dedicated CSV parser. Don't try to re-invent the wheel. – DontKnowMuchBut Getting Better Jul 10 '20 at 12:02
  • Do you have any examples for CSVParser because I always worked with split – omareltouny Jul 10 '20 at 13:53
  • Using split is fine if you absolutely *know* that the CSV data will remain simple and clean, but once you begin adding ambiguity or complexity of any sort, a parser library becomes essential. It is easy to find example code on this site using any CSV parser library of your choice. Simply do a Google search on the language that you're using (Java), the parser, and then add `site:stackoverflow.com` to the Google search to limit it to this site. – DontKnowMuchBut Getting Better Jul 10 '20 at 18:13
  • Okay, I will try using a parser. Thanks – omareltouny Jul 10 '20 at 21:03

0 Answers0