Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Generate string content for CSV with Apache Commons in Java instead of writing an actual file

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

StringWriter

You 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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!