Writing csv file using Buffered writer in Java

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

The code

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 a way to split a data column so that it is well readable?

EDIT

Instead of using a buffered writer, I use the CSVReader library for java. http://www.csvreader.com/java_csv.php Now the code

  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 formats the data correctly, but the problem occurs when I run the code a second time, when the second file is created, and the new file contains duplicate lines, for each line in the first file there are 2 lines in the second file with similar data.

I do not clean up any resources?

thanks

+8
java csv bufferedwriter
source share
3 answers

When writing a csv file, you need to attach your data as follows:

 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 in the data, it can cause a parsing problem and cause the data in different columns. Also make sure inside the finally block

 finally { //csvOutput is closed here } 
+1
source share

If you want to have a separate coloumn in Excel, for the fields that you wrote in the code, instead of fw.append(","); Just give fw.append(";"); It will work to set the header instead of creating a variable of type String f="Id, Name, Salary" and passing this variable to the fw.append(f); method fw.append(f); Just pass fields instead of creating a variable like fw.append("Id; Name; Salary") with a semicolon.

0
source share

From what I understand, your actual data does not line up in columns, because there is no way to find out how wide its column is.

You can try using tabs or predetermining the width of each column (for example, make all 10 characters).

-one
source share

All Articles