OpenCSV Avoid using FileWriter and return InputStream
Asked Answered
S

4

11

I am using OpenCsv and the new CSVWriter() method takes a Writer as an argument.

What I am trying to achieve is that to avoid writing to the file system and instead return an InputStream. I am not sure how to go about this. Not very familiar with Streams also I am using JAVA 7.

Is it possible that I could make the writeValues method return an InputStream and avoid writing the output on file system.

This is my working implementation:

private static void writeValues(String[][] cellValues) throws IOException {

    CSVWriter writer = new CSVWriter(new FileWriter("/tmp/myfile.csv"));
    for(String[] row : cellValues) { 
        writer.writeNext(row);
    }
    writer.close();
}

This is what I want to achieve. How to Convert the above method to avoid using a FileWriter.

private static InputStream writeValues(String[][] cellValues) throws IOException {

    InputStream inputStream = ?;

    CSVWriter writer = new CSVWriter(?);
    for(String[] row : cellValues) { 
        writer.writeNext(row);
    }
    writer.close();

    return inputStream;
}
Schizont answered 9/3, 2016 at 6:6 Comment(2)
What exactly is the point of returning a consumed ByteArrayInputStream()? And what was the result of this attempt?Poliard
I was able to figure out the problem. The CSVWriter needs to be closed before writing to file. I was not using the result of the return value as I was writing a file on disk.Schizont
C
9

Write to an OutpuStreamWriter itself writing to a ByteArrayOutputStream, and in the end, you'll have a byte array in memory (by calling getBytes() on the ByteArrayOutputStream).

You can then read from this byte array by opening a ByteArrayInputStream on the byte array.

Cornetcy answered 9/3, 2016 at 7:11 Comment(1)
Can you share your code here as to how you used OutputStreamWriter? I am in the same boat trying to avoid writing to a file and directly return InputStream.Ow
J
19

An example implementation to convert Beans to a byte array:

public static byte[] getBeansAsByteArray(final List<YourBean> beans) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
    CSVWriter writer = new CSVWriter(streamWriter);

    StatefulBeanToCsv<YourBean> beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
    beanToCsv.write(beans);
    streamWriter.flush();
    
    return stream.toByteArray();
}
Jensen answered 24/3, 2020 at 12:36 Comment(0)
C
9

Write to an OutpuStreamWriter itself writing to a ByteArrayOutputStream, and in the end, you'll have a byte array in memory (by calling getBytes() on the ByteArrayOutputStream).

You can then read from this byte array by opening a ByteArrayInputStream on the byte array.

Cornetcy answered 9/3, 2016 at 7:11 Comment(1)
Can you share your code here as to how you used OutputStreamWriter? I am in the same boat trying to avoid writing to a file and directly return InputStream.Ow
K
5

Grab a working snippet without StatefulBeanToCsv which I do not prefer.

 String[] headers = { "id", "result", "searchTerm"};
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
 CSVWriter writer = new CSVWriter(streamWriter);
 writer.writeNext(headers);
 // listOfRows -> business logic collection containing data for csv rows, as you can see I've separated the declaration of headers 
 listOfRows.forEach(it -> {
        writer.writeNext(new String[] {it.getId().toString(), it.getResult(), it.getSearchTerm()});
 });

 streamWriter.flush();
 byte[] byteArrayOutputStream = stream.toByteArray();
Kinnikinnick answered 7/2, 2022 at 6:47 Comment(0)
F
2

This is correct implementation

public static byte[] getBeansAsByteArray(List<YourBean> beans) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
    CSVWriter writer = new CSVWriter(streamWriter);

    StatefulBeanToCsv<YourBean> beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
    beanToCsv.write(beans);
    streamWriter.flush();

    return stream.toByteArray();
}
Fetter answered 6/1, 2021 at 8:59 Comment(1)
@MichaelKemmerzell, see this line CSVWriter writer = new CSVWriter(streamWriter);Fetter

© 2022 - 2024 — McMap. All rights reserved.