Quick and easy-of-use handling of comma-separated values (CSV) as described in RFC 4180.
Common Format and MIME Type for Comma-Separated Values (CSV) Files
Note: Currently, QuickCsv is not available on Maven Central.
To setup on your local repository, you need to follow this steps:
$ mvn clean install
Add the dependency in your pom.xml
<dependency>
<groupId>net.apercova</groupId>
<artifactId>quickcsv</artifactId>
<version>1.0.1712</version>
</dependency>
See API: (https://apercova.github.io/QuickCsv/apidocs/)
See docs on the wiki: (https://github.com/apercova/QuickCsv/wiki)
See complete usecases code: (https://github.com/apercova/QuickCsv/tree/master/usecases)
Reader reader = null;
try {
//Getting a reader for CsvFile.csv
reader = new InputStreamReader(
new FileInputStream("CsvFile.csv"),
Charset.forName("utf-8"));
//Values are read as a row list
List<List<String>> values = CsvReader.read(reader);
} catch (IOException e) {
logger.log(Level.SEVERE, "Can't perform reading", e);
} catch (CsvReaderException e) {
logger.log(Level.SEVERE, "Can't perform reading", e);
} finally {
try {
if(reader != null)
reader.close();
reader = null;
} catch (IOException e) {
logger.log(Level.FINE, "Unable to close reader", e);
}
}
try (
//Getting a reader for CsvFile.csv
Reader reader = new InputStreamReader(
new FileInputStream("CsvFile.csv"),
Charset.forName("utf-8"));
) {
//Values are read as a row list
List<List<String>> values = CsvReader.read(reader);
} catch (IOException | CsvReaderException e) {
logger.log(Level.SEVERE, "Can't perform reading", e);
}
See more csv reading usecases here.
Writer writer = null;
try {
//Getting a writer for Countries.csv
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Countries.csv"),
Charset.forName("utf-8")));
List<List<String>> values = new LinkedList<List<String>>();
values.add(Arrays.asList(new String[] {"ISO_CODE","NAME","CAPITAL"}));
values.add(Arrays.asList(new String[] {"US","United States of America",""}));
values.add(Arrays.asList(new String[] {"MX","Estados Unidos Mexicanos","Ciudad de México, \"CDMX\""}));
values.add(Arrays.asList(new String[] {"AU","Austalia","Sidney"}));
//Writing out values
CsvWriter.write(writer, values, true);//autoflush
//also direct flush
writer.flush();
} catch(IOException e) {
logger.log(Level.SEVERE, "Can't perform writing", e);
} catch (CsvWriterException e) {
logger.log(Level.SEVERE, "Can't perform writing", e);
} finally {
try {
if(writer != null)
writer.close();
writer = null;
} catch (IOException e) {
logger.log(Level.FINE, "Failed to close resource", e);
}
}
try (
//Getting a writer for Countries.csv
Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Countries.csv"),
Charset.forName("utf-8")));
){
List<List<String>> values = new LinkedList<List<String>>();
values.add(Arrays.asList(new String[] {"ISO_CODE","NAME","CAPITAL"}));
values.add(Arrays.asList(new String[] {"US","United States of America",""}));
values.add(Arrays.asList(new String[] {"MX","Estados Unidos Mexicanos","Ciudad de México, \"CDMX\""}));
values.add(Arrays.asList(new String[] {"AU","Austalia","Sidney"}));
//Writing out values
CsvWriter.write(writer, values, true);//autoflush
//also direct flush
writer.flush();
} catch(IOException | CsvWriterException e) {
logger.log(Level.SEVERE, "Can't perform writing", e);
}