View Javadoc

1   package net.sf.pzfilereader.examples.lowlevelparse;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.util.List;
7   
8   import net.sf.pzfilereader.util.ParserUtils;
9   
10  /*
11   * Created on Nov 27, 2005
12   *
13   */
14  
15  /**
16   * @author zepernick
17   * 
18   */
19  public class LowLevelParse {
20  
21      public static void main(final String[] args) {
22          String data = getDefaultDataFile();
23          try {
24              call(data);
25          } catch (Exception e) {
26              // TODO Auto-generated catch block
27              e.printStackTrace();
28          }
29      }
30  
31      public static String getDefaultDataFile() {
32          return "PEOPLE-CommaDelimitedWithQualifier.txt";
33      }
34  
35      public static void call(String data) throws Exception {
36          BufferedReader br = null;
37          FileReader fr = null;
38          final File textFile = new File(data);
39          String line = null;
40          List elements = null;
41  
42          try {
43              fr = new FileReader(textFile);
44              br = new BufferedReader(fr);
45  
46              while ((line = br.readLine()) != null) {
47                  if (line.trim().length() == 0) {
48                      continue;
49                  }
50  
51                  // tell the parser to split using a comma delimiter with a "
52                  // text qualifier. The text qualifier is optional, it can be
53                  // null
54                  // or empty
55                  elements = ParserUtils.splitLine(line, ',', '"', 10);
56  
57                  for (int i = 0; i < elements.size(); i++) {
58                      System.out.println("Column " + i + ": " + (String) elements.get(i));
59                  }
60  
61                  System.out.println("===========================================================================");
62  
63              }
64  
65          } catch (final Exception ex) {
66              ex.printStackTrace();
67          } finally {
68              try {
69                  if (br != null) {
70                      br.close();
71                  }
72                  if (fr != null) {
73                      fr.close();
74                  }
75              } catch (final Exception ignore) {
76              }
77          }
78  
79      }
80  }