0

How can I create CSV file in memory instead of physical location on drive by using OpenCSV

List<String[]> csvData = createCsvData("test@gmail.com", "123456", "444", "aaa", "bbb", "user");

        try (CSVWriter writer = new CSVWriter(new FileWriter("C:\\Test\\test.csv"),
                CSVWriter.DEFAULT_SEPARATOR,
                CSVWriter.NO_QUOTE_CHARACTER,
                CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                CSVWriter.DEFAULT_LINE_END)) {
            writer.writeAll(csvData);
        } catch (IOException e) {
            System.out.println(e);
        }
Ilan Miller
  • 323
  • 1
  • 3
  • 11
  • Wouldn't just setting your list in the variable as a string as opposed to the list be in fact creating the CSV in memory? `String csvData = "test@gmail.com,123456,444,aaa,bbb,user";` Or are you just trying to parse the CSV using OpenCSV in memory instead of from a file? Maybe I am not understanding the question correctly. – Hanlet Escaño Dec 02 '21 at 17:42
  • 1
    `CSVWriter` just accepts a `Writer`. You can replace your `FileWriter` with any thing you like, backed by anything you like `ByteArrayOutputStream` or `HttpResponseStream` if you wanted to send it directly. (assuming that is where you're going) – kendavidson Dec 03 '21 at 15:22
  • @HanletEscaño, I would like to send the CSV file directly without saving it locally. – Ilan Miller Dec 05 '21 at 07:54
  • Thanks @kendavidson, I'll check this out. – Ilan Miller Dec 05 '21 at 07:55
  • That's kinda a duplicate for the following question: https://stackoverflow.com/questions/35884180/opencsv-avoid-using-filewriter-and-return-inputstream – AElMehdi Oct 18 '22 at 16:54

0 Answers0