Skip to content

Instantly share code, notes, and snippets.

@Faz95210
Last active May 28, 2018 10:43
Show Gist options
  • Save Faz95210/aa0ad41ab0e9b745c46b492aebe1c208 to your computer and use it in GitHub Desktop.
Save Faz95210/aa0ad41ab0e9b745c46b492aebe1c208 to your computer and use it in GitHub Desktop.
[Parse CSV in Java] Example of how to parse a csv file in Java with apache Commons library #Java #CSV
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import java.io.IOException;
/*
** Gradle import : compile group: 'org.apache.commons', name: 'commons-csv', version: '1.5'
*/
private static void handleCsv(final String path) {
final Reader reader = new FileReader(path); // Read the path
try (CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) { // Try parsing the file
parser.foreach(records -> { // Iterate over each line of the csv
final String someValue = records.get(column); // get the value at column in csv
});
} catch (IOException ex) {
//Handle the error your way
}
}
public static void main(String[] args) {
handleCsv("path/to/your/file.csv")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment