<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
private static void writeCsv() throws IOException {
String fileNamePath = "C:\\Users\\LIVIA\\Desktop\\TEMP\\a.csv";
CSVPrinter printer = new CSVPrinter(new BufferedWriter(new FileWriter(fileNamePath)), CSVFormat.DEFAULT);
printer.printRecord("1","1","2");
printer.close(true);
}
private static void writeZip() throws IOException {
String fileNamePath = "C:\\Users\\LIVIA\\Desktop\\TEMP\\a.csv.zip";
ZipOutputStream zipOut = null;
try {
System.out.println(fileNamePath);
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fileNamePath)));
final String filename = fileNamePath.substring(fileNamePath.lastIndexOf(File.separator) + 1,
fileNamePath.lastIndexOf('.'));
zipOut.putNextEntry(new ZipEntry(filename));
CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(zipOut), CSVFormat.DEFAULT);
printer.printRecord("1", "2", "3");
printer.flush();
} catch (IOException e) {
throw e;
} finally {
if (zipOut != null){
zipOut.closeEntry();
zipOut.finish();
zipOut.close();
}
}
}
private static void writeGz() throws IOException {
String fileNamePath = "C:\\Users\\LIVIA\\Desktop\\TEMP\\a.csv.gz";
GZIPOutputStream gzOut = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(fileNamePath)));
CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(gzOut), CSVFormat.DEFAULT);
printer.printRecord("1", "2", "3");
printer.close(true);
gzOut.flush();
gzOut.finish();
gzOut.close();
}