JTextFileParser
JTextFileParser is a java package built for simplifing delimited text files parsing. It gives you a quick way for parsing a delimited text file, managing rows and row’s fields.
To use this package you need JDK 1.3 or greater.
This project is released under a GNU General Public License v3
Downloads
This project is hosted on Google Code.
Please visit the project page to download binary releases and source code.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Examples
Here you’ll find some examples on using jtextfileparse.
Example 1
Objective: parse a text file, check if the rows have all the expected number of fields and create a new text file with a subset of rows and fields with a different field separator:
The original text file
row1 one two three four five six seven eight nine ten row2 one two three four five six seven eight nine ten row3 one two three four five six seven eight nine ten row4 one two three four five six seven eight nine ten row5 one two three four five six seven eight nine ten row6 one two three four five six seven eight nine ten row7 one two three four five six seven eight nine ten row8 one two three four five six seven eight nine ten row9 one two three four five six seven eight nine ten row10 one two three four five six seven eight nine ten
The code
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; import net.melandri.jtextfileparser.beans.Row; import net.melandri.jtextfileparser.beans.TextFile; import net.melandri.jtextfileparser.filters.TextFileFilter; public class Test { public static void main(String[] args) { try { TextFile file = new TextFile("mytextfile.txt","C:\\Temp\\","\t"); ArrayList wrongRows = file.checkRowsLength(11); if (wrongRows.size() == 0){ TextFileFilter filter = new TextFileFilter(); filter.setFileName("mytextfilecopy.txt"); filter.setFilePath("C:\\Temp\\"); filter.setRows(new int[]{0,1,2,3,4,5}); filter.setFields(new int[]{2,5,7}); filter.setSeparator("*"); file.write(filter); }else{ Iterator it = wrongRows.iterator(); while (it.hasNext()){ Row thisRow = (Row)it.next(); System.out.println("Row " + thisRow.getNumber() + " has " + thisRow.getFields().length + " fields"); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
The output
The code above will generate a new text file like this
two*five*seven two*five*seven two*five*seven two*five*seven two*five*seven two*five*seven
Example 2
Objective: parse a text file and generate an html report including every row but only fields 0,5,6 and 9.
The original text file
row1 one two three four five six seven eight nine ten row2 one two three four five six seven eight nine ten row3 one two three four five six seven eight nine ten row4 one two three four five six seven eight nine ten row5 one two three four five six seven eight nine ten row6 one two three four five six seven eight nine ten row7 one two three four five six seven eight nine ten row8 one two three four five six seven eight nine ten row9 one two three four five six seven eight nine ten row10 one two three four five six seven eight nine ten
The code
import java.io.FileNotFoundException; import net.melandri.jtextfileparser.beans.TextFile; import net.melandri.jtextfileparser.filters.HTMLFileFilter; public class Test { public static void main(String[] args) { try { TextFile file = new TextFile("test.txt","C:\\Temp","\t",false); HTMLFileFilter filter = new HTMLFileFilter(); filter.setFilePath("C:\\Temp"); filter.setFileName("filtered.html"); filter.setPageTitle("My report"); filter.setFields(new int[]{0,5,6,9}); file.write(filter); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
The output
The code above will generate an HTML file like this:
<!-- body{font-family: arial, verdana, helvetica; font-size: 12px; padding: 10px; margin: 0;} .jtable{width: 100%; border: 1px solid #e1e1e1;border-collapse:collapse} tr.even{background: #f1f1f1;} td.jtd{padding: 3px;border: 1px solid #e1e1e1;} td.num{width: 40px;text-align:right;font-weight: bold;background: #638c9c;color: #FFFFFF;} --> <table id="table_0" class="jtable" border="0" cellspacing="0" cellpadding="0"> <tbody> <tr id="r0" class="odd"> <td id="r0_c0" class="jtd r0 c0">row1</td> <td id="r0_c1" class="jtd r0 c1">five</td> <td id="r0_c2" class="jtd r0 c2">six</td> <td id="r0_c3" class="jtd r0 c3">nine</td> </tr> <tr id="r1" class="even"> <td id="r1_c0" class="jtd r1 c0">row2</td> <td id="r1_c1" class="jtd r1 c1">five</td> <td id="r1_c2" class="jtd r1 c2">six</td> <td id="r1_c3" class="jtd r1 c3">nine</td> </tr> <tr id="r2" class="odd"> <td id="r2_c0" class="jtd r2 c0">row3</td> <td id="r2_c1" class="jtd r2 c1">five</td> <td id="r2_c2" class="jtd r2 c2">six</td> <td id="r2_c3" class="jtd r2 c3">nine</td> </tr> <tr id="r3" class="even"> <td id="r3_c0" class="jtd r3 c0">row4</td> <td id="r3_c1" class="jtd r3 c1">five</td> <td id="r3_c2" class="jtd r3 c2">six</td> <td id="r3_c3" class="jtd r3 c3">nine</td> </tr> <tr id="r4" class="odd"> <td id="r4_c0" class="jtd r4 c0">row5</td> <td id="r4_c1" class="jtd r4 c1">five</td> <td id="r4_c2" class="jtd r4 c2">six</td> <td id="r4_c3" class="jtd r4 c3">nine</td> </tr> <tr id="r5" class="even"> <td id="r5_c0" class="jtd r5 c0">row6</td> <td id="r5_c1" class="jtd r5 c1">five</td> <td id="r5_c2" class="jtd r5 c2">six</td> <td id="r5_c3" class="jtd r5 c3">nine</td> </tr> <tr id="r6" class="odd"> <td id="r6_c0" class="jtd r6 c0">row7</td> <td id="r6_c1" class="jtd r6 c1">five</td> <td id="r6_c2" class="jtd r6 c2">six</td> <td id="r6_c3" class="jtd r6 c3">nine</td> </tr> <tr id="r7" class="even"> <td id="r7_c0" class="jtd r7 c0">row8</td> <td id="r7_c1" class="jtd r7 c1">five</td> <td id="r7_c2" class="jtd r7 c2">six</td> <td id="r7_c3" class="jtd r7 c3">nine</td> </tr> <tr id="r8" class="odd"> <td id="r8_c0" class="jtd r8 c0">row9</td> <td id="r8_c1" class="jtd r8 c1">five</td> <td id="r8_c2" class="jtd r8 c2">six</td> <td id="r8_c3" class="jtd r8 c3">nine</td> </tr> <tr id="r9" class="even"> <td id="r9_c0" class="jtd r9 c0">row10</td> <td id="r9_c1" class="jtd r9 c1">five</td> <td id="r9_c2" class="jtd r9 c2">six</td> <td id="r9_c3" class="jtd r9 c3">nine</td> </tr> </tbody></table>
Version history
1.3.1 – 06.17.2008
- Added a new way to specify output parameters whe creating files from a TextFile object: now you can use TextFileFilter? and HTMLFileFilter. Check out the code and the documentation for more info.
1.3 – 06.12.2008
- Added the option to build a TextFile? object from an ArrayList? of Row objects
- Rewritten the code for accessing files: added the FileManager? class
- Improved the HTML export function: now you’ll get nicely formatted tables and valid XHTML markup. You can also specify your custom stylesheet.
- Code optimization and cleanup
1.2 – 03.12.2008
- Added the option to ignore the first line of the file. Now you can create a TextFile? object specifing the hasHeader parameter (true|false). If you set hasHeader to true you’ll be able to access the header row using the method getHeaderRow() and the first row returned by the method getRows() will be the second row of the file.
- Added the method replaceRows(ArrayList? rows) in the TextFile? class. This method will replace all rows in the TextFile?? object
- Deprecated the method getSimpleRow in the Row class. From now on you should use the method getStringRow
1.1 – 01.26.2008
- Added a method to export the text file to an HTML page
- Added a method to export selected rows to an HTML page
- Added a method to export selected rows and selected fields to an HTML page
- Some code cleanup
1.0 – 01.26.2008
- First public release