This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes output newline to CSV file how to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "output newline to CSV file how to" Watch "output newline to CSV file how to" New topic
Author

output newline to CSV file how to

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I would like to enter a new item into a CSV file but all my items being entered are all staying on one line in the CSV file. I would like each new item to show up on a new line and I can't seem to find the code to make it work. I've tried the \n and that didn't work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44515
    
  34
You probably don’t need to call flush(); if you look here, you will find out why.
There are methods in the BufferedWriter class which can help with new lines. You are better off buffering your FileWriter with a BufferedWriter. There is an example in the BufferedWriter API. BufferedWriter has a write() method which writes a String, but I think you would do better to put all your Strings together with a StringBuilder, and call its toString() method. That is because you get better performance with fewer calls to access the file. Write one line at a time rather than one word at a time.
Khunga Eusuph Qolo
Greenhorn

Joined: Apr 08, 2014
Posts: 1
it works if you use it with FileWriter...It really does!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44515
    
  34
Welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output newline to CSV file how to
 
It's not a secret anymore!