Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing a csv file using Buffered writer in Java

I am writing a csv file using buffered writer in java. My data is written correctly but I want to have different columns under which the data comes, Currently it is writing each instance of date in one row but not separated by columns.

The code is

  DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");  
        File file = new File( dirName + "\\"+ df.format(new Date()) +"_Statistics.csv");  
        if ( !file.exists() )
            file.createNewFile();

        FileWriter fw = new FileWriter(file);
        writer = new BufferedWriter( fw );
        writer.write("Message Source");
        writer.write("Message Name");
        writer.write("Component");
        writer.write("Occurance");
        writer.write("Message Payload");
        writer.write("Bandwidth (Payload)");
        writer.write("Message Payload with Header");
        writer.write("Bandwidth (Payload with Header)");
        writer.newLine();
        for (Signal signal : messages) {
            writer.write(signal.getSource());
            writer.write(signal.getName());
            writer.write(signal.getComponent());
            writer.write(Integer.toString(signal.getOccurance()));
            writer.write(Integer.toString(signal.getSize()));
            writer.write(Float.toString(signal.getBandwidth()));
            writer.write(Integer.toString(signal.getSizewithHeader()));
            writer.write(Float.toString(signal.getBandwidthWithHeader()));
            writer.newLine();
        }
        writer.flush();
        writer.close();
        fw.close();

Is there any way to separate the data column wise so that it is properly readable?

EDIT

Instead of using buffered writer I use CSVReader library for java. http://www.csvreader.com/java_csv.php The code now is

  DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");  
        File file = new File( dirName + "\\"+ df.format(new Date()) +"_Statistics.csv");  
        if ( !file.exists() )
            file.createNewFile();

        // Use FileWriter constructor that specifies open for appending
        CsvWriter csvOutput = new CsvWriter(new FileWriter(file, true), ',');

        //Create Header for CSV
        csvOutput.write("Message Source");
        csvOutput.write("Message Name");
        csvOutput.write("Component");
        csvOutput.write("Occurance");
        csvOutput.write("Message Payload");
        csvOutput.write("Bandwidth (Payload)");
        csvOutput.write("Message Payload with Header");
        csvOutput.write("Bandwidth (Payload with Header)");
        csvOutput.endRecord();
        for (Signal signal : messages) {
            csvOutput.write(signal.getSource());
            csvOutput.write(signal.getName());
            csvOutput.write(signal.getComponent());
            csvOutput.write(Integer.toString(signal.getOccurance()));
            csvOutput.write(Integer.toString(signal.getSize()));
            csvOutput.write(Float.toString(signal.getBandwidth()));
            csvOutput.write(Integer.toString(signal.getSizewithHeader()));
            csvOutput.write(Float.toString(signal.getBandwidthWithHeader()));
            csvOutput.endRecord();
        }
        csvOutput.flush();
        csvOutput.close();

It properly formats the data but the problem is now when I run the code for 2nd time a 2nd file is created and the new file contains duplicated rows, for every row in the first file there are 2 rows in the 2nd file with similar data.

Is that I am not cleaning some resources ?

Thanks

like image 709
Wearybands Avatar asked Sep 26 '13 11:09

Wearybands


1 Answers

When writing a csv file you need to enclose your data like this

csvOutput.write("\"");
csvOutput.write(signal.getSource());
csvOutput.write("\"");
csvOutput.write(",\"")
csvOutput.write("\"");
csvOutput.write(signal.getName()
csvOutput.write("\"");

And you need to do this because if there is a comma within a data it would cause a problem while parsing and cause data to be lodged in a different columns. Also make sure inside finally block

finally {
//csvOutput is closed here

}
like image 144
shashwatZing Avatar answered Sep 21 '22 01:09

shashwatZing