-1

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

  1. I don't want use OBJECT_LIST_SAMPLE , but I want create a temp file (File.creteTempFile), use it to write the strings
  2. In the end, I want download directly this file, for the user.

Please help me!

Thanks

Gjord83
  • 311
  • 1
  • 5
  • 17
  • Then create a [temp file](https://stackoverflow.com/questions/26860167/java-safe-way-to-create-a-temp-file) then based on how you communicate with the user, reply to the request with the file content, this can also be found with google. Right now, you have a working solution and some idea to update, but don't have research on it, start with that (first is simple) – AxelH Nov 20 '17 at 11:10
  • Check what `File.createTempFiles` return before complaining please. And for the second part, "_based on how you communicate with the user_" ... since I don't know, I can't help. – AxelH Nov 20 '17 at 11:40
  • Hi, I edited my code, adding: File temp = File.createTempFile("tempfile", ".csv"); BufferedWriter writer = new BufferedWriter(new FileWriter(temp)); But now the tmp file is empty. And also, I don't know how to reply to the request with file content – Gjord83 Nov 20 '17 at 11:46

1 Answers1

0

How to create a BufferedWriter on a temporary file using Files.createTempFile

Path tempDir = Files.createTempFile("Products", "sample.csv");
BufferedWriter writer = Files.newBufferedWriter(tempDir, StandardOpenOption.DELETE_ON_CLOSE);

Here is a quick example :

public static void main(String[] args) throws Exception {
    Path p = Files.createTempFile("Test", ".tmp");
    try(
            BufferedWriter writer = Files.newBufferedWriter(p, StandardOpenOption.DELETE_ON_CLOSE)
            ){
        System.out.println(p);
        for(int i = 0; i < 26; ++i){
            writer.append((char)(i+'a'));
        }

    }
}

Creating a "c:\Temp\Test#########.tmp" containing :

 abcdefghijklmnopqrstuvwxyz

For the second part, there is not enough information.

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • I modified my code and now I do: File temp = File.createTempFile("tempfile", ".csv"); BufferedWriter writer = new BufferedWriter(new FileWriter(temp)); but the tmp file is empty – Gjord83 Nov 20 '17 at 11:47
  • @Gjord83 I can't debug your code, all I can say is that the code provided here is a working solution. I have added the option to delete the file when the `close` method will be called (so be careful on what you want to do about this). – AxelH Nov 20 '17 at 12:00
  • Thanks, and how to download this created file? I mean, I want, that the user, download automatically the file – Gjord83 Nov 20 '17 at 12:08
  • @Gjord83 You keep asking but I still don't have any clue on how your users are communicating with that method... – AxelH Nov 20 '17 at 12:09
  • I am using vaadin 8, and there is a button to manage the export of products. When he clicks the button , there is a method wich writes the csv. When the csv is written, I would to open the dialog which asks "open or save" – Gjord83 Nov 20 '17 at 13:13
  • @Gjord83 First time you mention Vaadin... I can't help on that but check [taht link](https://stackoverflow.com/questions/21937516/vaadin-download-the-file-save-as) or some similar – AxelH Nov 20 '17 at 13:15
  • Yes, it's first time that I mentioned Vaadin, because I don't think it's strictly related with vaadin. I have a file in tmp directory, and I want to permit the user download the file, in the end of creation of csv file. Anyway, you was very kind! Thanks for all – Gjord83 Nov 20 '17 at 13:18