• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Output while loop results to CSV

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic