in my Java program, I would like to use the Apache Commons library to generate the content of a CSV file, but not actually creating the file.
I only want to have the String content of the file, and later, write the file using that content using an existing method.
However in all examples of code, it is necessary to define in the target csv file before hand, giving its path and name, but I don't have it, at this moment of the program flow. Is it possible to just get the String for the future csv file, and handle the real file creation independently? Thank you.
StringWriterYou need the file when you want to write to the file. You could pass the file's content around your programm or store it somewhere until you want to write to the file.
However if you want to write your CSV to String and not to a File you could try to use a StringWriter instead of a FileWriter.
Something like (not compiled, might not be complete)
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter , CSVFormat.DEFAULT
.withHeader("Header1", "Header2"));
csvPrinter.printRecord("abc", "ghf");
String csvString = stringWriter.toString();