I am using apache.commons.csv to create a CSV file. When generating the file ,I want to parse a header with the method .withHeader(). The values I parse should have quotes like "FILENAME". To do that I want to use an enum class. However I am not quite sure how it works to add quotes around the name. I tested it and with that code:
private enum Header{
FILENAME("\"FILENAME\""),
TEST("\"Test\"");
private final String string;
Header(String string){
this.string = string;
}
}
public void addHeader(File outFile) throws IOException{
CSVPrinter printer = new CSVPrinter(outFile, CSVFormat.DEFAULT.withHeader(Header.class));
}
However when outfile is created it only contains the header without quotes. Does anyone have an idea how to solve it? I know I use a string array as header but I would like to avoid that, because I have a lot of values an the string would get quite long.
As suggested by George Nechifor, we can use
CSVFormat#withHeader(String...)for better flexibility. To avoid duplication and hardcoding, of course we should use the value from enum, with a simple mapping.And will result in
"""FILENAME""","""Test"""An runnable example is as follows(with apache commons-csv 1.8):
Update from the comment
it seems what OP want is the header get quoted instead of header containing double quote. We may use
withQuoteMode(QuoteMode.ALL)and remove\"ingetCsvHeader.The result will be
"FILENAME","Test"Please note that this will also quote the record in csv. If we only want to quote the header, please refer to CSVPrinter remove quotes only from header, which is doing in reverse way, but the tricks still apply.