-2
@Value("classpath:tpls/Non-PnL_Template_Export_Order_Cross_Checking.csv")
private org.springframework.core.io.Resource exportFileOrderTpl;

I create an output stream of a CSVWriter

InputStream inputStream = exportFileOrderTpl.getInputStream();

String outputFileName = "file.csv"

var fileOutputStream = new FileOutputStream(outputFileName);

IOUtils.copy(inputStream, fileOutputStream);

var outputStreamWriter = new OutputStreamWriter(fileOutputStream,StandardCharsets.UTF_8);

var writer = new CSVWriter(outputStreamWriter)

String [] rows = {"1","2","3"}
writer.writeNext(rows);

var outputStream = new ByteArrayOutputStream();
outputStream.writeTo(fileOutputStream);

But my outputStream is empty. How to fix it!. Thank you

Loint
  • 3,560
  • 7
  • 26
  • 46
  • 2
    `writer` was opened on what…? – g00se Aug 11 '21 at 16:04
  • @g00se: I updated on my question, my missing sr. – Loint Aug 11 '21 at 16:05
  • I'm not sure what you're doing with those last two lines but I don't see `writer` being closed – g00se Aug 11 '21 at 16:09
  • Which is the final destination (`file.csv`) ? How many output-sinks do you have (`fileOutputStream`, `outputStreamWriter`, `writer`, `outputStream`) ? Please research via [search `[opencsv] write csv` here](https://stackoverflow.com/search?q=%5Bopencsv%5D%20write%20csv) and on a [tutorial for OpenCSV](https://www.baeldung.com/opencsv#2-the-csvwriter). – hc_dev Aug 11 '21 at 16:11
  • Might help to arrange your code in _coherent_ blocks (to group logical what belongs together) ... right now all statements are separated by blank lines which makes them appear"randomly choosen" – hc_dev Aug 11 '21 at 16:30
  • 1
    @hc_dev: CSVWriter will create a file .csv on my local machine, I'm wanting using output stream to no need create a file .csv on my machine. So any method support for this ? – Loint Aug 11 '21 at 16:53
  • 1
    `try(var writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputFileName),StandardCharsets.UTF_8)))) { ... }` ` is what I'd do – g00se Aug 11 '21 at 16:54
  • @Loint, Then update your question and explain there what you want, e.g. ask "How to use CSVWriter's output without needing a file?" Then answer could be [`StringWriter` for obtaining a String](https://docs.oracle.com/javase/7/docs/api/java/io/StringWriter.html) or [`PrintWriter` for connecting to some required output-stream](https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html) as text-based output passed to CSVWriter. – hc_dev Aug 11 '21 at 17:24

1 Answers1

0

Your issues

Not closing streams

The data like your rows is written to a buffer (in outputStreamWriter). Once you flush or close the stream(-writer), it gets piped.

Too much streams/sinks

You just created an empty outputStream and then (a) write it to another, (b) checking if it is empty. Does that make sense?

var outputStream = new ByteArrayOutputStream();
outputStream.writeTo(fileOutputStream);

At this point you could check your only used final sink fileOutputStream for bytes that have gotten flushed.

Combining two inputs

From your code, you want to combine two inputs into one output (stream or file). So you are connecting the CSVWriter to an already filled outputStream

// create final output file and connect a stream as sink: fileOutputStream
String outputFileName = "file.csv"
var fileOutputStream = new FileOutputStream(outputFileName);

// copy first input from given CSV file to sink: fileOutputStream
InputStream inputStream = exportFileOrderTpl.getInputStream();
IOUtils.copy(inputStream, fileOutputStream);

// connect additional CSV input to sink: fileOutputStream
var outputStreamWriter = new OutputStreamWriter(fileOutputStream,StandardCharsets.UTF_8);
var writer = new CSVWriter(outputStreamWriter)

// write rows via CSV to sink: fileOutputStream
String [] rows = {"1","2","3"}
writer.writeNext(rows);

// close writer including connected streams: fileOutputStream
writer.close();

Writing CSV-file using OpenCSV

Like in Baeldungs introduction to OpenCSV's CSVWriter, you could use the method in given example and slightly modify to write, your single row or many rows to a specified file path:

public void writeRows(List<String[]> stringArray, Path path) throws Exception {
    CSVWriter writer = new CSVWriter(new FileWriter(path.toString()));
    for (String[] array : stringArray) {
        writer.writeNext(array);
    }
    
    writer.close();
}

// define your rows consisting of many columns
String[] rowA = {"1","2","3"}
String[] rowB = {"3","4","5"}
List<String[]> rows = List.of(rowA, rowB);

// define your output file
String outputFileName = "file.csv";
Path outputPath = Paths.get(outputFileName);

// write the rows to file by calling the method
writeRows(rows, outputPath)

Note: Only the writer is used as output-sink. Everything IO-related is handled by OpenCSV. No separate output-streams needed.

hc_dev
  • 8,389
  • 1
  • 26
  • 38