Article View: borland.public.jbuilder.java.language
Article #14454Re: How to make CSV file?
From: Paul Furbacher [TeamB]
Date: Tue, 31 Aug 1999 00:00
Date: Tue, 31 Aug 1999 00:00
57 lines
2626 bytes
2626 bytes
Kobus du Toit wrote:
> What is CSV file[?]
CSV stands for "comma-separated values". It is a standard
format for exporting a database table to a text file. An alternative
format is "tab-delimited" text, which as you might guess, uses the
tab character to separate column values.
> File create you use the File class.
Yes, perhaps, but this really isn't very helpful.
> HooLee originally wrote:
> > How to make CSV file in Java? (file create?)
It depends upon what you are starting with. If you have
some values which are in a row-and-column order, you
simply write those values to a file, separating the column
values with commas. For instance, if you have a grid control
which is not connected to a dataset, you would get the
items from the grid control and write them out to a file.
The strategy would be to open a file and iterate through each
row, creating a string from the column data, separating the
values with a comma and ending with an operating system-specific
line separator.
// create the FileWriter
FileWriter writer = new FileWriter(filename);
// get the strings from the grid control
String [][] items = gridControl.getItems();
// get the system-specific line terminator character(s)
String lineSeparator = System.getProperty("line.separator");
for ( int row = 0; row < items.length; row++ ) {
String line = "";
for ( int col = 0; col < items[0].length; col++ )
line = line + ",";
// remove the comma at the end of the "line"
line = line.substring(0, line.length() -1) + lineSeparator;
writer.write(line);
}
writer.close();
If you have a dataset, you can create an instance of
borland.jbcl.dataset.TextDataFile, and set its filename
property. In order to have the TextDataFile write a CSV file,
you set the delimiter to a comma:
TextDataFile tdf = new TextDataFile();
tdf.setFilename(theFilename);
tdf.setDelimiter(",");
tdf.save(myDataset);
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<37CC42C7.51723A49@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7pfmte$30v17@forums.borland.com> <37C66F80.F8BF790C@iname.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,940b2d228abc4ae1 X-Google-Attributes: gidf74b3,public From: "Paul Furbacher [TeamB]" <pf@bilbo.bio.purdue.edu> Subject: Re: How to make CSV file? Date: 1999/08/31 Message-ID: <37CC42C7.51723A49@bilbo.bio.purdue.edu> X-Deja-AN: 519454198 Content-Transfer-Encoding: 7bit References: <7pfmte$30v17@forums.borland.com> <37C66F80.F8BF790C@iname.com> Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pf@bilbo.bio.purdue.edu Newsgroups: borland.public.jbuilder.java.language