//define field lengths FixedWidthFields fields = new FixedWidthFields(); accountFields.addField("ID", 10); accountFields.addField("Bank", 8); accountFields.addField("AccountNumber", 15); accountFields.addField("Swift", 12); //configure the parser FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); //many options here, check the tutorial settings.getFormat().setLineSeparator("\n"); //We can now parse all rows FixedWidthParser parser = new FixedWidthParser(settings); List<String[]> rows = parser.parseAll(new File("/path/to/file.txt"));
Lookup[] getLookbehindFormats() { return Lookup.getLookupFormats(lookbehindFormats, getFormat()); }
/** * Returns the sequence of paddings used by each field of each record. * * @return the sequence of paddings used by each field of each record. */ char[] getFieldPaddings() { if (fieldLengths == null) { return null; } return fieldLengths.getFieldPaddings(this.getFormat()); }
FixedWidthFieldLengths lengths = new FixedWidthFieldLengths(4, 5, 40, 40, 8); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); //sets the character used for padding unwritten spaces in the file settings.getFormat().setPadding('_'); // creates a fixed-width parser with the given settings FixedWidthParser parser = new FixedWidthParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new FileReader(yourFile));
// creates the sequence of field lengths in the file to be parsed FixedWidthFields fields = new FixedWidthFields(4, 5, 40, 40, 8); // creates the default settings for a fixed width parser FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); // many settings here, check the tutorial. //sets the character used for padding unwritten spaces in the file settings.getFormat().setPadding('_'); // creates a fixed-width parser with the given settings FixedWidthParser parser = new FixedWidthParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new File("path/to/fixed.txt")));
Lookup[] getLookaheadFormats() { return Lookup.getLookupFormats(lookaheadFormats, getFormat()); }
private void init() { fields = new ArrayList<FixedWidthField>(); List<String> headers = new ArrayList<String>(); List<Integer> fieldWidth = new ArrayList<Integer>(); for (String tmp : fieldDescription.split(",")) { String[] fieldTuple = tmp.split(":(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1); FixedWidthField field = new FixedWidthField(); field.setName(fieldTuple[0]); field.setType(fieldTuple[1]); field.setWidth(Integer.parseInt(fieldTuple[2])); headers.add(fieldTuple[0]); fieldWidth.add(Integer.parseInt(fieldTuple[2])); if (field.getType() == FIELD_TYPE.DATE) { if (fieldTuple.length > 3) { field.setDateFormat(fieldTuple[3].replace("\"", "")); } else { logger.error("Date format is missing for the field {}", field.getName()); throw new RuntimeException("Missing date format"); } } fields.add(field); } header = headers.toArray(new String[headers.size()]); int[] width = Ints.toArray(fieldWidth); FixedWidthFields lengths = new FixedWidthFields(header, width); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); settings.getFormat().setPadding(this.padding); fixedWidthParser = new FixedWidthParser(settings); }
private void init() { fields = new ArrayList<FixedWidthField>(); List<String> headers = new ArrayList<String>(); List<Integer> fieldWidth = new ArrayList<Integer>(); for (String tmp : fieldDescription.split(",")) { String[] fieldTuple = tmp.split(":(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1); FixedWidthField field = new FixedWidthField(); field.setName(fieldTuple[0]); field.setType(fieldTuple[1]); field.setWidth(Integer.parseInt(fieldTuple[2])); headers.add(fieldTuple[0]); fieldWidth.add(Integer.parseInt(fieldTuple[2])); if (field.getType() == FIELD_TYPE.DATE) { if (fieldTuple.length > 3) { field.setDateFormat(fieldTuple[3].replace("\"", "")); } else { logger.error("Date format is missing for the field {}", field.getName()); throw new RuntimeException("Missing date format"); } } fields.add(field); } header = headers.toArray(new String[headers.size()]); int[] width = Ints.toArray(fieldWidth); FixedWidthFields lengths = new FixedWidthFields(header, width); FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths); settings.getFormat().setPadding(this.padding); fixedWidthParser = new FixedWidthParser(settings); }