Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes I/O and Streams and the fly likes Writing CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing CSV file" Watch "Writing CSV file" New topic
Author

Writing CSV file

Mike Jeya
Ranch Hand

Joined: Apr 15, 2004
Posts: 54
Hi,
we need to create the CSV file . problem is some column value have comma in it, because of which the file format is changed.
can you please let me know the procedure to take care of comma values too,


RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.writeBytes(new StringBuffer(strUID+","
+strFirstName+","+strLastName+","+strMail+","+strTelephone+","+groupName+"\n").toString());
raf.close();

please advise me.....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42038
    
  64
You need to enclose all cells contents by quotes, so that they look like this:

There are other characters that need to be treated especially (like doubles quotes and newlines), so you may be better off using one of the readily available CSV libraries. The http://faq.javaranch.com/java/AccessingFileFormats lists several.


Ping & DNS - my free Android networking tools app
Rajasekhar Pentakota
Ranch Hand

Joined: Aug 28, 2007
Posts: 38



Thanks
Raja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing CSV file