Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda