/** * Creates a new instance initialized from {@link FixedWidth} annotations in the fields and methods of a given class. Note that * all fields should additionally have the {@link Parsed} annotation to configure header names and/or their positions. * * Only setter methods will be considered as fields. * * @param beanClass the class whose {@link FixedWidth} annotations will be processed to configure this field list. * * @return a new {@link FixedWidthFields} instance built with the {@link FixedWidth} annotations found in the given class' attributes and methods (excluding getters) */ public static FixedWidthFields forParsing(Class beanClass) { return new FixedWidthFields(beanClass, MethodFilter.ONLY_SETTERS); }
/** * Creates a new instance initialized from {@link FixedWidth} annotations in the fields and methods of a given class. Note that * all fields should additionally have the {@link Parsed} annotation to configure header names and/or their positions. * * Only getter methods will be considered as fields. * * @param beanClass the class whose {@link FixedWidth} annotations will be processed to configure this field list. * * @return a new {@link FixedWidthFields} instance built with the {@link FixedWidth} annotations found in the given class' attributes and methods (excluding setters) */ public static FixedWidthFields forWriting(Class beanClass) { return new FixedWidthFields(beanClass, MethodFilter.ONLY_GETTERS); }
//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"));
// 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")));
/** * Creates the {@link com.univocity.parsers.fixed.FixedWidthFields} instance based on the headers and field * lengths. * * @return new {@code FixedWidthFields} based on the header and field lengths. */ private FixedWidthFields createFixedWidthFields() { // Ensure that the field lengths have been defined. if (fieldLengths == null) { throw new IllegalArgumentException("The fieldLengths must have been defined in order to use the fixed-width format."); } // If there's no header then we only use their length if (headers == null) { return new FixedWidthFields(fieldLengths); } // Use both headers and field lengths (same size and no duplicate headers) if (fieldLengths.length != headers.length) { throw new IllegalArgumentException("The headers and fieldLengths must have the same number of element in order to use the fixed-width format."); } LinkedHashMap<String, Integer> fields = new LinkedHashMap<>(); for (int i = 0; i < headers.length; i++) { fields.put(headers[i], fieldLengths[i]); } if (fields.size() != headers.length) { throw new IllegalArgumentException("The headers cannot have duplicates in order to use the fixed-width format."); } return new FixedWidthFields(fields); }
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); }
protected void adjustColumnLengths(String[] headers, int[] lengths){ if(getWriterSettings().getFieldLengths() == null) { getWriterSettings().setFieldLengths(new FixedWidthFields(headers, lengths)); } }
/** * Function to create a univocity Parser */ private void createUnivocityParser() { List<FixedWidthSchema.Field> fields = schema.getFields(); FixedWidthFields fieldWidthFields = new FixedWidthFields(); for (int i = 0; i < fields.size(); i++) { FixedWidthSchema.Field currentField = fields.get(i); int fieldLength = currentField.getFieldLength(); FieldAlignment currentFieldAlignment; if (currentField.getAlignment().equalsIgnoreCase("centre")) { currentFieldAlignment = FieldAlignment.CENTER; } else if (currentField.getAlignment().equalsIgnoreCase("left")) { currentFieldAlignment = FieldAlignment.LEFT; } else { currentFieldAlignment = FieldAlignment.RIGHT; } fieldWidthFields.addField(currentField.getName(), fieldLength, currentFieldAlignment, currentField.getPadding()); } FixedWidthParserSettings settings = new FixedWidthParserSettings(fieldWidthFields); univocityParser = new com.univocity.parsers.fixed.FixedWidthParser(settings); }
/** * Function to create a univocity Parser */ private void createUnivocityParser() { List<FixedWidthSchema.Field> fields = schema.getFields(); FixedWidthFields fieldWidthFields = new FixedWidthFields(); for (int i = 0; i < fields.size(); i++) { FixedWidthSchema.Field currentField = fields.get(i); int fieldLength = currentField.getFieldLength(); FieldAlignment currentFieldAlignment; if (currentField.getAlignment().equalsIgnoreCase("centre")) { currentFieldAlignment = FieldAlignment.CENTER; } else if (currentField.getAlignment().equalsIgnoreCase("left")) { currentFieldAlignment = FieldAlignment.LEFT; } else { currentFieldAlignment = FieldAlignment.RIGHT; } fieldWidthFields.addField(currentField.getName(), fieldLength, currentFieldAlignment, currentField.getPadding()); } FixedWidthParserSettings settings = new FixedWidthParserSettings(fieldWidthFields); univocityParser = new com.univocity.parsers.fixed.FixedWidthParser(settings); }
FixedWidthFields accountFields = new FixedWidthFields(); accountFields.addField("ID", 10); //account ID has length of 10 accountFields.addField("Bank", 8); //bank name has length of 8 accountFields.addField("AccountNumber", 15); //etc accountFields.addField("Swift", 12); //Format for clients' records FixedWidthFields clientFields = new FixedWidthFields(); clientFields.addField("Lookahead", 5); //clients have their lookahead in a separate column clientFields.addField("ClientID", 15, FieldAlignment.RIGHT, '0'); //let's pad client ID's with leading zeroes. clientFields.addField("Name", 20); FixedWidthWriterSettings settings = new FixedWidthWriterSettings(); settings.getFormat().setLineSeparator("\n"); settings.getFormat().setPadding('_'); //If a record starts with C#, it's a client record, so we associate "C#" with the client format. settings.addFormatForLookahead("C#", clientFields); //Rows starting with #A should be written using the account format settings.addFormatForLookahead("A#", accountFields); StringWriter out = new StringWriter(); //Let's write FixedWidthWriter writer = new FixedWidthWriter(out, settings); writer.writeRow(new Object[]{"C#",23234, "Miss Foo"}); writer.writeRow(new Object[]{"A#23234", "HSBC", "123433-000", "HSBCAUS"}); writer.writeRow(new Object[]{"A#234", "HSBC", "222343-130", "HSBCCAD"}); writer.writeRow(new Object[]{"C#",322, "Mr Bar"}); writer.writeRow(new Object[]{"A#1234", "CITI", "213343-130", "CITICAD"}); writer.close(); System.out.println(out.toString());