1
package com;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import com.opencsv.CSVWriter;
import com.opencsv.CSVReader;

public class Sample2 {
    public static void main(String args[]) throws IOException
    {
        CSVReader csvReader = null;  
        String[] employeeDetails ;        
        CSVWriter  csvWriter = new CSVWriter(new FileWriter("D:\\sample\\myfile.csv",true));
        csvReader = new CSVReader(new FileReader("D:\\sample\\source.csv"));          
        try
        {        

            employeeDetails = csvReader.readNext();               
            while ((employeeDetails = csvReader.readNext()) != null ) {               

                    System.out.println(Arrays.toString(employeeDetails));                   
                    csvWriter.writeNext(employeeDetails);
                }               

        }catch(Exception ee)
            {
                ee.printStackTrace();
            }
        }
}

I have my above java code It read data from source.csv file and also display in the console . It created myfile.csv ,but same contents it didn't write in the csv file Anyone have any idea on this

2
  • 1
    Did you try to close your FileWriter and CsvWriter? Try using try-with resources Commented Jan 23, 2019 at 13:56
  • 1
    Not sure but may be you have to flush and close the writer Commented Jan 23, 2019 at 14:05

2 Answers 2

1

CSVWriter implements Flushable.Working Solution is already present in @Stephan Hogenboom's answer. I will answer why didn't it write in your case,

From the javadocs of Flushable interface,

A Flushable is a destination of data that can be flushed. The flush method is invoked to write any buffered output to the underlying stream.

For performance reasons, all data is to be written into a Buffer instead of File temporarily. Once you call the flush() method, it flushes the data already present in the buffer into your file(this is where disk I/O happens, not when you call writeNext() ).

As mentioned on doc of flush() in java.io.Writer.

Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination.

Sign up to request clarification or add additional context in comments.

3 Comments

Good explanation!
Thanks @StephanHogenboom :)
I must say javadocs has answers for anything just if we know to read it right! :P
0

The issue is that you don't close your output resources, try this code:

public static void main(String args[]) throws IOException {
String[] employeeDetails;

try (CSVWriter csvWriter = new CSVWriter(new FileWriter("D:\\sample\\myfile.csv", true));
    CSVReader csvReader = new CSVReader(new FileReader("D:\\sample\\source.csv"));
    ) {

  while ((employeeDetails = csvReader.readNext()) != null) {

    System.out.println(Arrays.toString(employeeDetails));
    csvWriter.writeNext(employeeDetails);
  }
}
catch (Exception ee) {
  ee.printStackTrace(); //perhaps you should also log the error?
}
}

Also take a look at this question Is closing the resources always important?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.