View Javadoc

1   package net.sf.flatpack.examples.delimitedcolumnnamesinfile;
2   
3   /*
4    * Created on Nov 27, 2005
5    *
6    */
7   
8   import java.io.File;
9   
10  import net.sf.flatpack.DataError;
11  import net.sf.flatpack.DataSet;
12  import net.sf.flatpack.DefaultParserFactory;
13  import net.sf.flatpack.Parser;
14  import net.sf.flatpack.ordering.OrderBy;
15  import net.sf.flatpack.ordering.OrderColumn;
16  
17  /**
18   * @author zepernick
19   * 
20   * TODO To change the template for this generated type comment go to Window -
21   * Preferences - Java - Code Style - Code Templates
22   */
23  public class DelimitedColumnNamesInFile {
24      public static void main(final String[] args) throws Exception {
25          String[] colNames = null;
26          OrderBy orderby = null;
27  
28          // delimited by a comma
29          // text qualified by double quotes
30          // ignore first record
31          final Parser pzparser = DefaultParserFactory.getInstance().newDelimitedParser(new File("PEOPLE-CommaDelimitedWithQualifier.txt"), ',', '"');
32          final DataSet ds = pzparser.parse();
33  
34          // re order the data set by last name
35          orderby = new OrderBy();
36          orderby.addOrderColumn(new OrderColumn("CITY", false));
37          orderby.addOrderColumn(new OrderColumn("LASTNAME", true));
38          ds.orderRows(orderby);
39  
40          colNames = ds.getColumns();
41  
42          while (ds.next()) {
43              for (int i = 0; i < colNames.length; i++) {
44                  System.out.println("COLUMN NAME: " + colNames[i] + " VALUE: " + ds.getString(colNames[i]));
45              }
46  
47              System.out.println("===========================================================================");
48          }
49  
50          if (ds.getErrors() != null && ds.getErrors().size() > 0) {
51              System.out.println("FOUND ERRORS IN FILE....");
52              for (int i = 0; i < ds.getErrors().size(); i++) {
53                  final DataError de = (DataError) ds.getErrors().get(i);
54                  System.out.println("Error: " + de.getErrorDesc() + " Line: " + de.getLineNo());
55              }
56          }
57  
58      }
59  }