I'm currently using org.apache.commons.csv.CSVPrinter to create a CSV File in my project. When I started a new project and aimed to reuse this code:

BufferedWriter successWriter = Files.newBufferedWriter(Paths.get(successFile + "_" + sDate + ".csv")); csvPrinter = new CSVPrinter(successWriter, CSVFormat.DEFAULT.withDelimiter(delimiter.charAt(0)) .withRecordSeparator("\n").withEscape('\\').withQuoteMode(QuoteMode.NONE)); 

I get the warning that all "with*" methods are deprecated.

Apache states to use "set*" instead. But since these are CSVBuilder methods, I'm not sure how to use them properly.

Can you provide an example on how to use them best with the given code?

Many thanks in advance!

1 Answer

As per the javadoc ()

DEFAULT is a CSVFormat instance with predefined builder in it.
You can update the builder or create one from scratch.

Here is a quick example :

 BufferedWriter successWriter = Files.newBufferedWriter(Paths.get( "C:\\temp\\testingtest.csv")); char delimiter = ';'; CSVPrinter csvPrinter = new CSVPrinter(successWriter, // Creates a new default builder. CSVFormat.Builder.create() // use setters .setDelimiter(delimiter).setRecordSeparator("\n").setEscape('\\').setQuoteMode(QuoteMode.NONE).setHeader("h1","h2").build()); csvPrinter.printRecord("1", "2"); csvPrinter.flush(); 

NB : apache commons-csv 1.9 which is the last version is marked with vulnerabilities from dependencies:

1. CVE-2022-23221
2. CVE-2021-42392
3. CVE-2021-23463

you may not want to use it in production.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.