KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > misc > CSVParser


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.misc;
34
35 import java.io.File JavaDoc;
36 import java.io.FileReader JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.UnsupportedEncodingException JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.Reader JavaDoc;
44 import java.io.InputStreamReader JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46
47 import java.sql.Connection JavaDoc;
48 import java.sql.SQLException JavaDoc;
49 import java.sql.Date JavaDoc;
50 import java.sql.CallableStatement JavaDoc;
51
52 import com.knowgate.debug.DebugFile;
53 import com.knowgate.jdc.JDCConnection;
54 import com.knowgate.misc.Gadgets;
55 import com.knowgate.dataobjs.DB;
56 import com.knowgate.dataobjs.DBBind;
57 import com.knowgate.dataobjs.DBPersist;
58
59 /**
60  * <p>Delimited Text Parser</p>
61  * <p>Parses a delimited text file into a memory array</p>
62  * @author Sergio Montoro Ten
63  * @version 3.0
64  */

65 public class CSVParser {
66
67   private char cBuffer[]; // Buffer interno que contiene los caracteres del fichero a parsear
68
private int iBuffer; // Longuitud del buffer interno
69
private String JavaDoc ColNames[]; // Nombres de columnas leidos del descriptor de fichero
70
private int RowPointers[]; // Punteros al inicio de cada línea en el buffer interno
71
private int ColPointers[][]; // Punteros al inicio de cada columna en el buffer interno
72
private int iCols; // Número de columnas contadas en el descriptor
73
private int iRows; // Número de columnas contadas en el descriptor
74
private int iErrLine; // Línea del fichero donde se produjo el último error de parseo
75
private char cDelimiter;
76   private boolean bQuoted;
77   private String JavaDoc sCharSet;
78
79   // ----------------------------------------------------------
80

81   public CSVParser() {
82     iBuffer = 0;
83     sCharSet = null;
84   }
85
86   // ----------------------------------------------------------
87

88   /**
89    * Create CSV Parser and set encoding to be used
90    * @param sCharSetName Name of charset encoding
91    */

92   public CSVParser(String JavaDoc sCharSetName) {
93     iBuffer = 0;
94     sCharSet = sCharSetName;
95   }
96
97   // ----------------------------------------------------------
98

99   public String JavaDoc charSet() {
100     return sCharSet;
101   }
102
103   // ----------------------------------------------------------
104

105   public void charSet(String JavaDoc sCharSetName) {
106     sCharSet = sCharSetName;
107   }
108
109   // ----------------------------------------------------------
110

111   /**
112    * Get line count
113    * @return int
114    * @since 3.0
115    */

116   public int getLineCount() {
117     return iRows;
118   }
119
120   // ----------------------------------------------------------
121

122   /**
123    * Get column count
124    * @return int
125    * @since 3.0
126    */

127   public int getColumnCount() {
128     return iCols;
129   }
130
131   // ----------------------------------------------------------
132

133   public int errorLine() {
134     return iErrLine;
135   }
136
137   // ----------------------------------------------------------
138

139   public char getDelimiter() {
140     return cDelimiter;
141   }
142
143   // ----------------------------------------------------------
144

145   /**
146    * <p>Parse data from a character array</p>
147    * Parsed values are stored at an internal array in this CSVParser.
148    * @param sFileDescriptor A list of column names separated by ',' ';' '|' '`' or '\t'.
149    * Column names may be quoted. Lines are delimiter by '\n' characters<br>
150    * Example 1) tx_mail,tx_name,tx_surname<br>
151    * Example 2) "tx_name","tx_surname","tx_salutation"<br>
152    * @throws ArrayIndexOutOfBoundsException Delimited values for a file is greater
153    * than columns specified at descriptor.
154    * @throws RuntimeException If delimiter is not one of { ',' ';' '|' '`' or '\t' }
155    * @throws NullPointerException if sFileDescriptor is <b>null</b>
156    * @throws IllegalArgumentException if sFileDescriptor is ""
157    */

158
159   public void parseData(char[] aCharData, String JavaDoc sFileDescriptor)
160     throws ArrayIndexOutOfBoundsException JavaDoc, RuntimeException JavaDoc,
161            NullPointerException JavaDoc,IllegalArgumentException JavaDoc {
162
163     boolean bIgnore;
164     char cAt;
165
166     if (DebugFile.trace) {
167       DebugFile.writeln("Begin CSVParser.parseData(char[" + String.valueOf(aCharData.length) + "], \"" + sFileDescriptor + "\")");
168       DebugFile.incIdent();
169     }
170
171     bQuoted = false;
172     cDelimiter = (char)0;
173     bIgnore = false;
174
175     if (aCharData!=cBuffer) {
176       iBuffer = aCharData.length;
177       cBuffer = new char[iBuffer];
178       System.arraycopy(aCharData, 0, cBuffer, 0, iBuffer);
179     }
180
181     iErrLine = 0;
182
183     if (DebugFile.trace) DebugFile.writeln("trimming leading whitespaces");
184
185     // Ignorar los espacios en blanco al final del fichero
186
for (int p=iBuffer-1; p>=0; p--) {
187       cAt = cBuffer[p];
188       if (cAt==' ' || cAt=='\n' || cAt=='\r' || cAt=='\t')
189         iBuffer--;
190       else
191         break;
192     }
193
194     if (iBuffer==0) {
195       iRows = 0;
196       if (DebugFile.trace) {
197         DebugFile.decIdent();
198         DebugFile.writeln("End CSVParser.parseData() : zero length array");
199       }
200       return;
201     }
202
203     // Si el primer caracter no en blanco es comillas,
204
// entonces se entiende que los campos van entrecomillados
205

206     int iFileDescLen = sFileDescriptor.length();
207
208     for (int p=0; p<iBuffer; p++) {
209       cAt = sFileDescriptor.charAt(p);
210
211       if (cAt!=' ' && cAt!='\t' && cAt!='\n' && cAt!='\r') {
212         bQuoted = (cAt == '"');
213         break;
214       }
215     } // next
216

217     if (DebugFile.trace) {
218       if (bQuoted) DebugFile.writeln("asume quoted identifiers");
219     }
220
221     // Inferir el delimitador
222

223     for (int p=0; p<iFileDescLen && cDelimiter==(char)0; p++) {
224
225       cAt = sFileDescriptor.charAt(p);
226
227       if (cAt=='"') bIgnore = !bIgnore;
228       if (!bIgnore) {
229         switch (cAt) {
230           case ',':
231             cDelimiter = ',';
232             break;
233           case ';':
234             cDelimiter = ';';
235             break;
236           case '|':
237             cDelimiter = '|';
238             break;
239           case '`':
240             cDelimiter = '`';
241             break;
242           case '\t':
243             cDelimiter = '\t';
244             break;
245         } // end switch()
246
} // fi ()
247
} // next
248

249     if (DebugFile.trace) {
250       if (cDelimiter == (char)0) DebugFile.writeln("error: cannot assign a valid column delimiter");
251     }
252
253     if (cDelimiter == (char)0)
254       throw new RuntimeException JavaDoc("Cannot assign a valid column delimiter");
255
256     // Almacenar los nombres de campo y contar el número de columnas
257
ColNames = Gadgets.split(sFileDescriptor, new String JavaDoc(new char[]{cDelimiter}));
258     iCols = ColNames.length;
259
260     if (DebugFile.trace) DebugFile.writeln("descriptor has " + String.valueOf(iCols) + " columns");
261
262     if (bQuoted)
263       for (int c=0; c<iCols; c++)
264         ColNames[c] = (ColNames[c].replace('"',' ')).trim();
265
266     // Contar el número de filas a partir de los saltos de línea
267
iRows = 1;
268     for (int p=0; p<iBuffer; p++) {
269       if (cBuffer[p]=='\n') iRows++;
270     } // next
271

272     if (DebugFile.trace) DebugFile.writeln("input data has " + String.valueOf(iRows) + " lines");
273
274     RowPointers = new int[iRows];
275     ColPointers = new int[iRows][iCols];
276
277     int iRow = 0, iCol = 0;
278
279     if (DebugFile.trace) DebugFile.writeln("parsing line 0");
280
281     RowPointers[iRow] = 0;
282     ColPointers[iRow][iCol] = 0;
283
284     bIgnore = false;
285
286     for (int p=0; p<iBuffer; p++) {
287
288       cAt = cBuffer[p];
289
290       if (cAt=='"' && bQuoted) bIgnore = !bIgnore;
291
292       if (!bIgnore) {
293         if (cAt==cDelimiter) {
294           iCol++;
295           if (iCol>=iCols) {
296             iErrLine = iRow+1;
297             throw new ArrayIndexOutOfBoundsException JavaDoc("Columns count mismatch for line " + String.valueOf(iErrLine) + " expected " + String.valueOf(iCols) + " but found more.");
298           }
299           else
300             ColPointers[iRow][iCol] = p+1;
301         }
302         else if (cAt=='\n') {
303           if (iCol!=iCols-1) {
304             iErrLine = iRow+1;
305             throw new ArrayIndexOutOfBoundsException JavaDoc("Columns count mismatch for line " + String.valueOf(iErrLine) + " expected " + String.valueOf(iCols) + " and found only " + String.valueOf(iCol+1));
306           }
307           iRow++;
308           iCol = 0;
309
310           if (DebugFile.trace) DebugFile.writeln("parsing line " + String.valueOf(iRow));
311
312           RowPointers[iRow] = p+1;
313           ColPointers[iRow][iCol] = p+1;
314         }
315       } // fi (bIgnore)
316
} // next
317

318     iErrLine = 0;
319
320     if (DebugFile.trace) {
321       DebugFile.decIdent();
322       DebugFile.writeln("End CSVParser.parseData()");
323     }
324   } // parseData
325

326   // ----------------------------------------------------------
327

328   /**
329    * <p>Parse a delimited text file</p>
330    * Parsed values are stored at an internal array in this CSVParser.<br>
331    * File is readed using the character set specifid at constructor
332    * @param oFile CSV File
333    * @param sFileDescriptor A list of column names separated by ',' ';' '|' '`' or '\t'.
334    * Column names may be quoted. Lines are delimiter by '\n' characters<br>
335    * Example 1) tx_mail,tx_name,tx_surname<br>
336    * Example 2) "tx_name","tx_surname","tx_salutation"<br>
337    * @throws IOException
338    * @throws FileNotFoundException
339    * @throws ArrayIndexOutOfBoundsException Delimited values for a file is greater
340    * than columns specified at descriptor.
341    * @throws RuntimeException If delimiter is not one of { ',' ';' '|' '`' or '\t' }
342    * @throws NullPointerException if oFile or sFileDescriptor are <b>null</b>
343    * @throws IllegalArgumentException if sFileDescriptor is ""
344    * @throws UnsupportedEncodingException
345    * @since 3.0
346    */

347   public void parseFile(File JavaDoc oFile, String JavaDoc sFileDescriptor)
348       throws ArrayIndexOutOfBoundsException JavaDoc,IOException JavaDoc,FileNotFoundException JavaDoc,
349              RuntimeException JavaDoc,NullPointerException JavaDoc,IllegalArgumentException JavaDoc,
350              UnsupportedEncodingException JavaDoc {
351
352     Reader JavaDoc oReader;
353
354     if (oFile==null)
355       throw new NullPointerException JavaDoc("CSVParser.parseFile() File parameter may not be null");
356
357     if (DebugFile.trace) {
358       DebugFile.writeln("Begin CSVParser.parseFile(\"" + oFile.getAbsolutePath() + "\",\"" + sFileDescriptor + "\")");
359       DebugFile.incIdent();
360     }
361
362     if (sFileDescriptor==null) {
363       if (DebugFile.trace) DebugFile.decIdent();
364       throw new NullPointerException JavaDoc("CSVParser.parseFile() File Descriptor parameter may not be null");
365     }
366
367     if (sFileDescriptor.trim().length()==0) {
368       if (DebugFile.trace) DebugFile.decIdent();
369       throw new IllegalArgumentException JavaDoc("File Descriptor parameter may not be an empty string");
370     }
371
372     iErrLine = 0;
373
374     iBuffer = new Long JavaDoc(oFile.length()).intValue();
375
376     if (iBuffer==0) {
377       iRows = 0;
378       if (DebugFile.trace) {
379         DebugFile.decIdent();
380         DebugFile.writeln("End CSVParser.parseFile() : zero length file");
381       }
382       return;
383     }
384
385     cBuffer = new char[iBuffer];
386
387     if (null==sCharSet) {
388       oReader = new FileReader JavaDoc(oFile);
389     } else {
390       oReader = new InputStreamReader JavaDoc(new FileInputStream JavaDoc(oFile), sCharSet);
391     }
392     oReader.read(cBuffer);
393     oReader.close();
394     oReader = null;
395
396     parseData (cBuffer, sFileDescriptor);
397
398     if (DebugFile.trace) {
399       DebugFile.decIdent();
400       DebugFile.writeln("End CSVParser.parseFile()");
401     }
402   } // parseFile
403

404   // ----------------------------------------------------------
405

406   /**
407    * <p>Parse a delimited text file</p>
408    * Parsed values are stored at an internal array in this CSVParser.
409    * @param sFilePath File Path
410    * @param sFileDescriptor A list of column names separated by ',' ';' '|' '`' or '\t'.
411    * Column names may be quoted. Lines are delimiter by '\n' characters<br>
412    * Example 1) tx_mail,tx_name,tx_surname<br>
413    * Example 2) "tx_name","tx_surname","tx_salutation"<br>
414    * @throws IOException
415    * @throws FileNotFoundException
416    * @throws ArrayIndexOutOfBoundsException Delimited values for a file is greater
417    * than columns specified at descriptor.
418    * @throws RuntimeException If delimiter is not one of { ',' ';' '|' '`' or '\t' }
419    * @throws NullPointerException if oFile or sFileDescriptor are <b>null</b>
420    * @throws IllegalArgumentException if sFileDescriptor is ""
421    * @throws UnsupportedEncodingException
422    */

423   public void parseFile(String JavaDoc sFilePath, String JavaDoc sFileDescriptor)
424       throws ArrayIndexOutOfBoundsException JavaDoc,IOException JavaDoc,FileNotFoundException JavaDoc,
425              RuntimeException JavaDoc,NullPointerException JavaDoc,IllegalArgumentException JavaDoc,
426              UnsupportedEncodingException JavaDoc {
427     parseFile (new File JavaDoc(sFilePath), sFileDescriptor);
428   }
429
430   // ----------------------------------------------------------
431

432   /**
433    * @param sColumnName Column Name
434    * @return Zero based index for column position or -1 if column was not found.
435    */

436   public int getColumnPosition(String JavaDoc sColumnName) {
437
438     if (DebugFile.trace) {
439       DebugFile.writeln("Begin CSVParser.getColumnPosition(" + sColumnName + ")");
440       DebugFile.incIdent();
441     }
442
443     int iPos = -1;
444
445     for (int c=0; c<iCols; c++) {
446       if (ColNames[c].equalsIgnoreCase(sColumnName)) {
447         iPos = c;
448         break;
449       }
450     } // next
451

452     if (DebugFile.trace) {
453       DebugFile.decIdent();
454       DebugFile.writeln("End CSVParser.getColumnPosition() : " + String.valueOf(iPos));
455     }
456
457     return iPos;
458   } // getColumnPosition
459

460   // ----------------------------------------------------------
461

462   /**
463    * <p>Get line from a parsed file.</p>
464    * Lines are delimited by the Line Feed (LF, CHAR(10), '\n') character
465    * @param iLine Line Number [0..getLineCount()-1]
466    * @return Full Text for Line. If iLine<0 or iLine>=getLineCount() then <b>null</b>
467    * @throws IllegalStateException If parseFile() has not been called prior to getLine()
468    * @throws UnsupportedEncodingException
469    */

470   public String JavaDoc getLine(int iLine) throws IllegalStateException JavaDoc, UnsupportedEncodingException JavaDoc {
471     String JavaDoc sRetVal;
472     int iStart, iEnd;
473
474     if (DebugFile.trace) {
475       DebugFile.writeln("Begin CSVParser.getLine(" + String.valueOf(iLine) + ")");
476       DebugFile.incIdent();
477     }
478
479     if (0 == iBuffer)
480       throw new IllegalStateException JavaDoc("Must call parseFile() on a valid non-empty delimited file before calling getField() method");
481
482     if (iLine<0 || iLine>iRows-1)
483
484       sRetVal = null;
485
486     else {
487
488       iStart = ColPointers[iLine][0];
489       iEnd = iBuffer;
490
491       // Search for line feed
492
for (int p=iStart; p<iBuffer; p++)
493         if (cBuffer[p]=='\n') {
494           iEnd = p;
495           break;
496         } // fi ()
497

498       if (iStart==iEnd)
499         sRetVal = "";
500       else {
501         // Remove last Carriage Return (CR, CHAR(13), '\r') character
502
if (iEnd-1>iStart) {
503           if (cBuffer[iEnd-1]=='\r') --iEnd;
504           if (iStart==iEnd)
505             sRetVal = "";
506           else
507             sRetVal = new String JavaDoc(cBuffer, iStart, iEnd - iStart);
508         }
509         else {
510           if (cBuffer[iStart]=='\r')
511             sRetVal = "";
512           else
513             sRetVal = new String JavaDoc(cBuffer, iStart, iEnd - iStart);
514         }
515       }
516
517     } // fi (iRow<0 || iRow>iRows-1)
518

519     if (DebugFile.trace) {
520       DebugFile.decIdent();
521       DebugFile.writeln("End CSVParser.getLine() : " + sRetVal);
522     }
523
524     return sRetVal;
525   } // getLine
526

527   // ----------------------------------------------------------
528

529   /**
530    * <p>Get value for a field at a given row and column.</p>
531    * Column indexes are zero based.<br>
532    * Row indexes range from 0 to getLineCount()-1.
533    * @param iCol Column Index
534    * @param iRow Row Index
535    * @return Field Value
536    * @throws IllegalStateException If parseFile() method was not called prior to
537    * getField()
538    * @throws ArrayIndexOutOfBoundsException If Column or Row Index is out of bounds.
539    * @throws StringIndexOutOfBoundsException If Row is malformed.
540    * @throws UnsupportedEncodingException If charset encoding name is not recognized.
541    */

542   public String JavaDoc getField(int iCol, int iRow)
543       throws IllegalStateException JavaDoc, ArrayIndexOutOfBoundsException JavaDoc,
544              StringIndexOutOfBoundsException JavaDoc, UnsupportedEncodingException JavaDoc {
545     int iStart;
546     int iEnd;
547     String JavaDoc sRetVal;
548
549     if (DebugFile.trace) {
550       DebugFile.writeln("Begin CSVParser.getField(" + String.valueOf(iCol) + "," + String.valueOf(iRow) + ")");
551       if (iBuffer>0) DebugFile.incIdent();
552     }
553
554     iErrLine = 0;
555
556     if (0 == iBuffer)
557       throw new IllegalStateException JavaDoc("Must call parseFile() on a valid non-empty delimited file before calling getField() method");
558
559     if (-1==iCol || -1==iRow) {
560       if (DebugFile.trace) {
561         DebugFile.decIdent();
562         DebugFile.writeln("End CSVParser.getField() : null");
563       }
564       return null;
565     }
566
567     iErrLine = iRow;
568
569     iStart = ColPointers[iRow][iCol];
570
571     if (DebugFile.trace) DebugFile.writeln("iStart=" + String.valueOf(iStart));
572
573     if (iCol<iCols-1)
574       iEnd = ColPointers[iRow][iCol+1]-1;
575     else if (iRow<iRows-1)
576       iEnd = ColPointers[iRow+1][0]-1;
577     else
578       iEnd = iBuffer;
579
580     if (DebugFile.trace) DebugFile.writeln("triming trailing spaces from " + String.valueOf(iEnd));
581
582     if (iEnd>0 && iEnd<iBuffer) {
583       if (bQuoted) {
584         while (cBuffer[iEnd - 1] == '\r' || cBuffer[iEnd - 1] == ' ' ||
585                cBuffer[iEnd - 1] == '\t')
586           if (--iEnd == 0)
587             break;
588       }
589       else {
590         if (cBuffer[iEnd-1]=='\r') iEnd--;
591       }
592     }
593     else if (iEnd<0)
594       iEnd = 0;
595
596     if (DebugFile.trace) DebugFile.writeln("iEnd=" + String.valueOf(iEnd));
597
598     if (iStart==iEnd)
599       sRetVal = "";
600     else if (bQuoted)
601         sRetVal = new String JavaDoc(cBuffer, iStart+1, iEnd-iStart-2);
602     else
603       sRetVal = new String JavaDoc(cBuffer, iStart, iEnd-iStart);
604
605     iErrLine = 0;
606
607     if (DebugFile.trace) {
608       DebugFile.decIdent();
609       DebugFile.writeln("End CSVParser.getField() : " + sRetVal);
610     }
611
612     return sRetVal;
613   } // getField
614

615   // ----------------------------------------------------------
616

617   /**
618    * <p>Get value for a field at a given row and column.</p>
619    * @param sCol Column name
620    * @param iRow Row position [0..getLineCount()-1]
621    * @throws IllegalStateException
622    * @throws ArrayIndexOutOfBoundsException
623    * @throws StringIndexOutOfBoundsException
624    * @throws UnsupportedEncodingException
625    * @return Field value
626    */

627   public String JavaDoc getField(String JavaDoc sCol, int iRow)
628     throws IllegalStateException JavaDoc, ArrayIndexOutOfBoundsException JavaDoc,
629            StringIndexOutOfBoundsException JavaDoc, UnsupportedEncodingException JavaDoc {
630
631     int iCol = getColumnPosition(sCol);
632
633     if (iCol==-1)
634       throw new ArrayIndexOutOfBoundsException JavaDoc ("Column " + sCol + " not found");
635
636     return getField (iCol, iRow);
637   }
638
639   // ----------------------------------------------------------
640

641   /**
642    * <p>Find first occurence of a value at a given column</p>
643    * Search is case sensitive
644    * @param iCol int Column index [0..getColumnCount()-1]
645    * @param sVal String Value sought
646    * @return int
647    * @throws UnsupportedEncodingException
648    * @since 3.0
649    */

650   public int find (int iCol, String JavaDoc sVal) throws UnsupportedEncodingException JavaDoc {
651     int iFound = -1;
652     int r = 0;
653     while (r<iRows) {
654       if (getField(iCol,r).equals(sVal)) {
655         iFound = r;
656         break;
657       }
658     } // wend
659
return iFound;
660   } // find
661

662   // ----------------------------------------------------------
663

664   /**
665    * <p>Find first occurence of a value at a given column</p>
666    * Search is case insensitive
667    * @param iCol int Column index [0..getColumnCount()-1]
668    * @param sVal String Value sought
669    * @return int
670    * @throws UnsupportedEncodingException
671    * @since 3.0
672    */

673   public int findi (int iCol, String JavaDoc sVal) throws UnsupportedEncodingException JavaDoc {
674     int iFound = -1;
675     int r = 0;
676     while (r<iRows) {
677       if (getField(iCol,r).equalsIgnoreCase(sVal)) {
678         iFound = r;
679         break;
680       }
681     } // wend
682
return iFound;
683   } // findi
684

685   // ----------------------------------------------------------
686

687   /**
688    * Write CSVParser matrix to an output stream
689    * @param oStrm OutputStream
690    * @throws IOException
691    * @since 3.0
692    */

693   public void writeToStream(OutputStream JavaDoc oStrm) throws IOException JavaDoc {
694     if (DebugFile.trace) {
695       DebugFile.writeln("Begin CSVParser.writeToStream([OutputStream])");
696       DebugFile.incIdent();
697     }
698
699     if (null!=sCharSet)
700       oStrm.write(new String JavaDoc(cBuffer).getBytes(sCharSet));
701     else
702       oStrm.write(new String JavaDoc(cBuffer).getBytes());
703
704     if (DebugFile.trace) {
705       DebugFile.decIdent();
706       DebugFile.writeln("End CSVParser.writeToStream()");
707     }
708   } // writeToStream
709

710   // ----------------------------------------------------------
711

712   /**
713    * Write CSVParser matrix to delimited text file
714    * @param oStrm OutputStream
715    * @throws IOException
716    * @throws SecurityException
717    * @since 3.0
718    */

719   public void writeToFile(String JavaDoc sFilePath) throws IOException JavaDoc, SecurityException JavaDoc {
720     if (DebugFile.trace) {
721       DebugFile.writeln("Begin CSVParser.writeToFile("+sFilePath+")");
722       DebugFile.incIdent();
723     }
724     FileOutputStream JavaDoc oOutStrm = new FileOutputStream JavaDoc(sFilePath);
725     BufferedOutputStream JavaDoc oOutBuff = new BufferedOutputStream JavaDoc(oOutStrm);
726
727     writeToStream(oOutBuff);
728
729     oOutBuff.close();
730     oOutStrm.close();
731
732     if (DebugFile.trace) {
733       DebugFile.decIdent();
734       DebugFile.writeln("End CSVParser.writeToFile()");
735     }
736   } // writeToFile
737

738 }
739
Popular Tags