File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Creating a CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a CSV file" Watch "Creating a CSV file" New topic

Creating a CSV file

sonu rai

Joined: May 23, 2007
Posts: 22
Hi All,

I want to create a csv file , I have to give heading and beside it there will be detail of that heading. such as Name is an heading and bellow it name of students will be. So, what can i do.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15041

Writing CSV files is very easy. "CSV" means "comma separated values". A CSV file consist of records; each line is a record, and the fields in each record are separated by commas.

You just write a program that opens the file for writing, first write the header line, then loop through the records, for each record write a line with the data for that record (fields separated by spaces), then close the file.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
CSV does not have a concept of headers. It has rows and columns of values. ANy program processing a CSV file is of course free to treat certain rows or cells in special ways (as if they were headers).

It gets a little more complicated that Jesper described, though. Sometimes the delimiter is not a comma but a semicolon, and if the delimiter occurs inside of a cell, then the cell contents need to be enclosed in double quotes. And if the contents contain double quotes or newline characters, then those need to be escaped.

Before you've coded up all these special cases, you might as well use a ready-made library for reading and writing CSVs. The AccessingFileFormats FAQ page lists several such libraries.
poorna prakash parvathala

Joined: Dec 18, 2006
Posts: 20
try this

it has the utility programs you can extract to suit your needs

Poorna Prakash<br /> <br />SCJP 1.4 (90%), SCWCD (planning)
I agree. Here's the link:
subject: Creating a CSV file
It's not a secret anymore!