Read and validate CSV files with Java.

Nithidol Vacharotayan
5 min readMar 1, 2024
Image by freepik

Scenario: The client has a business requirement to upload a CSV file and validate it before saving data into the database server.

A CSV (Comma-Separated Values) file is a simple, widely used file format for storing tabular data. It is a plain text file where each line represents a row of data, and the values within each row are separated by commas (or other delimiters like tabs, semicolons, etc.).

The hard part is not reading CSV, but the real hard part is validating data in a CSV file because the source data is plain text. The developer can’t control data inside the file, e.g., format or encoding. To ensure the developer uses the right encoding, the developer can use the Apache Tika or Icu4J libraries to auto-detect the encoding.

The CSV example file contains five columns separated by a comma(“,”).
column 1: index,
column 2: first name,
column 3: last name,
column 4: weight,
column 5: date of birth,

--

--

Nithidol Vacharotayan

Programming enthusiast with 10+ years of experience in Java loves sharing knowledge with others and exploring new technologies together!