I am creating a file csv with opencsv library, but I have some little difficulties. I followed a tutorial on line:
private void exportProducts() {
String OBJECT_LIST_SAMPLE = "C:/tmp/object-list-sample.csv";
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(OBJECT_LIST_SAMPLE));
CSVWriter csvWriter = new CSVWriter(writer,
CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
) {
String[] headerRecord = {"Name", "Email", "Phone", "Country"};
csvWriter.writeNext(headerRecord);
csvWriter.writeNext(new String[]{"Sundar Pichai ♥", "sundar.pichai@gmail.com", "+1-1111111111", "India"});
csvWriter.writeNext(new String[]{"Satya Nadella", "satya.nadella@outlook.com", "+1-1111111112", "India"});
} catch (IOException e) {
LOG.error("", e);
}
}
All good, the file is created correctly, but I want extend this one. I mean
- I don't want use OBJECT_LIST_SAMPLE , but I want create a temp file (File.creteTempFile), use it to write the strings
- In the end, I want download directly this file, for the user.
Please help me!
Thanks