x
login about faq

CSV File writing

I have array of values need to be write in csv

array {(x0,y0,z0),(x1,y1,z1),(x2,y2,z2)}

then I have column header as (a,b,c)

now format, which I want my array to be written in csv is

a x0 y0 z0 b x1 y1 z1 c x2 y2 z2

more ▼

asked Mar 23 at 07:09 PM

coolvibh\'s gravatar image

Vaibhav Shrivastava
1 3 3 3

(comments are locked)
10|600 characters needed characters left

2 answers: sort voted first

class Tokenizer

more ▼

answered Aug 12 at 09:09 PM

nicholas_jordan\'s gravatar image

nicholas_jordan
2 5

(comments are locked)
10|600 characters needed characters left

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException;

public class CSVWriter {

static String[][] str = { { "x0", "y0", "z0" }, { "x1", "y1", "z1" },
       { "x2", "y2", "z2" } };
static String[] header = { "a", "b", "c" };

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < str.length; i++) {
       String[] row = str[i];
       String headerVal = header[i];
       sb.append(headerVal);
       for (int j = 0; j < row.length; j++) {
         sb.append(",").append(row[j]);
       }
       sb.append("\n");
    }
    System.out.println(sb.toString());

    BufferedWriter out = null;
    try {
       out = new BufferedWriter(new FileWriter("test.csv"));
       out.write(sb.toString());
    } catch (IOException e) {
       e.printStackTrace();
    } finally {
       if (null != out) {
         try {
          out.close();
         } catch (IOException e) {
          e.printStackTrace();
         }
       }
    }
}

}

more ▼

answered Aug 17 at 02:29 PM

nikeshpl\'s gravatar image

Nikesh Pl
0

(comments are locked)
10|600 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

powered by AnswerHub - Enterprise Social Q&A