Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Output while loop results to CSV

 
James Brownie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ppls,

I have a little project that grabs some input data and then runs a while loop to generate the output,
what I would like to do is write the input data and while loop into a csv.

I know I can use the FileWriter api to append data into the csv

Just not sure how to go about getting all the while loop output into the csv.

This is what I have so far, (I have not started the csv build method yet)

Any direction would be greatly appreciated.


 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is not usually a good idea to access CSVs directly; if you look in our FAQ (under “Excel”) you can find some libraries to write and read CSVs. At this stage, you might do well to output all your text to the command line. Then you can see its format and demonstrate the creation of the lines is working.
To write simple text, you can use a file writer, but buffer it first, or you can use a Formatter. Go through the Java Tutorials and you will find examples of both. You can wrap a File object in a Formatter and write like that. Remember you must close writers with a finally block (or try‑with‑resources); if you forget to close them, not only might the file be locked, but also you might fail to write anything. You will find links to different ways to close streams in this post. That probably refers to readers, but you can doubtless change it to close your writer.
You want to move almost that code out of the main method; it is far too long already.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic