Java Reference
In-Depth Information
Solution
Use my CSV class or a regular expression (see Chapter 4 ).
Discussion
CSV is deceptive. It looks simple at first glance, but the values may be quoted or unquoted.
If quoted, they may further contain escaped quotes. This far exceeds the capabilities of the
StringTokenizer class (see Breaking Strings Into Words ). Either considerable Java coding
or the use of regular expressions is required. I'll show both ways.
First, a Java program. Assume for now that we have a class called CSV that has a no-argu-
ment constructor and a method called parse() that takes a string representing one line of the
input file. The parse() method returns a list of fields. For flexibility, the fields are returned
as a List , which you can process in any way you like:
CSVSimple.java
package
package com . darwinsys . csv ;
import
import java.util.List
java.util.List ;
/* Simple demo of CSV parser class. */
public
public class
class CSVSimple
CSVSimple {
public
public static
void main ( String [] args ) {
CSVImport parser = new
static void
new CSVImport ();
List < String > list = parser . parse (
"\"LU\",86.25,\"11/4/1998\",\"2:19PM\",+4.0625" );
for
for ( String word : list ) {
System . out . println ( word );
}
// Now try with a non-default separator
parser = new
new CSVImport ( '|' );
list = parser . parse (
"\"LU\"|86.25|\"11/4/1998\"|\"2:19PM\"|+4.0625" );
for
for ( String word : list ) {
System . out . println ( word );
}
}
}
Search WWH ::




Custom Search