Export array values to csv file java

arrays, csv, java

Solution

Assuming you're storing String in your array, I post an alternative solution:

BufferedWriter br = new BufferedWriter(new FileWriter("myfile.csv"));
StringBuilder sb = new StringBuilder();

// Append strings from array
for (String element : array) {
 sb.append(element);
 sb.append(",");
}

br.write(sb.toString());
br.close();

Best regards.

Problem

I just need help exporting array elements to a csv file. I don't know what's wrong with my code. Any help is gladly appreciated. Thanks. ``` for (int index = 0; index < cols.length; index++) { FileWriter fw = new FileWriter(newFileName); if (index == cols.length - 1) { fw.append(cols[index]); } else { fw.append(cols[index]); fw.append(","); } } ``` When I run this. Nothing happens to my csv file. Infact, it wipes everything of. Please help.

Original source