computer






 

Question by  Raju (23)

How do I write to a csv file using Java?

 
+7

Answer by  nickl2000 (540)

You would create a new File object with a .csv extension. Then you would create and instantiate new Writer objects (I like StreamWriters). Then create whatever Strings you want to write with commas in them to seperate the values, and write them out to the file using the Writer. Finally close the Writers

 
+5

Answer by  Sting1 (686)

A CSV file is just a normal text file, with comma-separated values written in it. Writing data to a CSV file is the exact same thing as writing data into a normal text file. Use the writer.append function.

 
+5

Answer by  Adrian27 (338)

You cannot do that, CSV files are not currently supported by Java. I recommend you find a good CSV transformer to help you with that.

 
+5

Answer by  Oliviero (81)

the same way you do normally. Just put the information in the right order and have the commas separating the variables.

 
+5

Answer by  nightwalker (105)

Using an output to file system making sure to name your file with a. csv and simple use a for-loop adding a comma at the end of each new entry.

 
+5

Answer by  MukeshSnehi (11)

import java. io. FileWriter; //add to import list import java. io. *; try { FileWriter writer = new FileWriter(fileName); for(int i = 0; i < results. length; i++) { for(int j = 0; j < results[i]. length; j++) { writer. append(results[i][j]); if(j < results[j]. length - 1) writer. append(','); else writer. append('\n'); } } } catch(IOException e) { e. printStackTrace(); }

 
+3

Answer by  gigo (1706)

In any case you need to import the java.io.* libraries. Then you can instanciate a FileWrite oject and use the function " objFW.append(',');" after each data entry.

 
You have 50 words left!