opencsv is a free and open source library for reading and writing
CSV files in Java. We need to have opencsv-2.3.jar or
later versions in classpath.
The following example shows reading an entire CSV file using opencsv.
/**
* @param args
* @throws IOException
*/ public static void main(String[] args) throws IOException {
/**
* Load CSV from classpath
*/
CSVReader csvReader = new CSVReader(new InputStreamReader(
ReadCSV.class.getClassLoader().getResourceAsStream("test1.csv")));
/**
* Reads the complete file into list of tokens.
*/
List<String[]> rowsAsTokens = csvReader.readAll();
Iterator<String[]> rowsAsTokensIt = rowsAsTokens.iterator();
while (rowsAsTokensIt.hasNext()) { for (String token : rowsAsTokensIt.next()) {
System.out.print(token + " ");
}
System.out.println();
}
}