Heavens, writing a CSV parser is trivial. It's simply a case of a
StringTokenizer in a for loop:
public ResultClass parse( InputStream in, String separatorChars)
throws IOException
{
ResultClass result = new ResultClass();
BufferedReader buffy =
new BufferedReader( new InputStreamReader( in));
for ( String line = buffy.readLine(); line != null;
line = buffy.readLine)
{
StringTokenizer tok =
new StringTokenizer( line, separatorChars);
while ( tok.hasMoreTokens())
{
// do something with result and
tok.nextToken()
}
}
/* consider (and document) whether it's your or the
caller's
* responsibility to close the stream; since you were
passed the
* stream I suggest it's the caller's */
return result;
}
As to what that ResultClass object should be, if the first line in your
CSV
may be column headers and each value in the first row is distinct then
probably what you want is a vector of maps where the keys of the maps are
the corresponding values from the first line; otherwise I'd probably just
return a vector of vectors.
Obviously you may not want to schlurp a whole CSV file into core memory at
one go; it may be better to produce a parser to which you can add
callbacks/listeners for the fields or patterns you are interested in. But
the general pattern is as given.
--
(e-mail address removed) (Simon Brooke)
http://www.jasmine.org.uk/~simon/
;; Let's have a moment of silence for all those Americans who are stuck
;; in traffic on their way to the gym to ride the stationary bicycle.
;; Rep. Earl Blumenauer (Dem, OR)