@Override protected FixedWidthWriterSettings createDefaultWriterSettings() { return new FixedWidthWriterSettings(); } }
/** * Clones this configuration object to reuse most user-provided settings. Properties that are specific to a given * input (such as header names and selection of fields) will be reset to their defaults. * * To obtain a full copy, use {@link #clone()}. * * @param fields the fixed-width field configuration to be used by the cloned settings object. * * @return a copy of the <em>general</em> configurations applied to the current instance. */ public final FixedWidthWriterSettings clone(FixedWidthFields fields) { return clone(true, fields); }
FixedWidthFormat format = settings.getFormat(); this.padding = format.getPadding(); this.defaultPadding = padding; this.fieldLengths = settings.getAllLengths(); this.fieldAlignments = settings.getFieldAlignments(); this.fieldPaddings = settings.getFieldPaddings(); this.ignore = settings.getFieldsToIgnore(); if (ignore != null) { for (int i = 0; i < ignore.length; i++) { this.lookaheadFormats = settings.getLookaheadFormats(); this.lookbehindFormats = settings.getLookbehindFormats(); this.defaultHeaderPadding = settings.getUseDefaultPaddingForHeaders(); this.defaultHeaderAlignment = settings.getDefaultAlignmentForHeaders(); super.enableNewlineAfterRecord(settings.getWriteLineSeparatorAfterRecord());
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());
settings.setNullValue("?"); settings.setRowWriterProcessor(new BeanWriterProcessor<TestBean>(TestBean.class)); settings.setHeaders("amount", "pending", "date", "quantity", "comments");
/** * You can only create an instance of this class by providing a definition of the field lengths of each record in the input. * <p> This must provided using an instance of {@link FixedWidthFields}. * * @param fieldLengths the instance of {@link FixedWidthFields} which provides the lengths of each field in the fixed-width records to be parsed * * @see FixedWidthFields */ public FixedWidthWriterSettings(FixedWidthFields fieldLengths) { setFieldLengths(fieldLengths); NormalizedString[] names = fieldLengths.getFieldNames(); if (names != null) { setHeaders(NormalizedString.toArray(names)); } }
/** * 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(getFormat()); }
protected void adjustColumnLengths(String[] headers, int[] lengths){ if(getWriterSettings().getFieldLengths() == null) { getWriterSettings().setFieldLengths(new FixedWidthFields(headers, lengths)); } }
Lookup[] getLookbehindFormats() { return Lookup.getLookupFormats(lookbehindFormats, getFormat()); }
/** * {@inheritDoc} */ @Override protected FixedWidthWriterSettings createWriterSettings() { return new FixedWidthWriterSettings(createFixedWidthFields()); }
Lookup[] getLookaheadFormats() { return Lookup.getLookupFormats(lookaheadFormats, getFormat()); }
/** * Clones this configuration object to reuse most user-provided settings. This includes the fixed-width field configuration, * but doesn't include other input-specific settings. This method is meant to be used internally only. * * @return a copy of all configurations applied to the current instance. * * @deprecated doesn't really make sense for fixed-width. . Use alternative method {@link #clone(FixedWidthFields)}. */ @Deprecated protected final FixedWidthWriterSettings clone(boolean clearInputSpecificSettings) { return clone(clearInputSpecificSettings, fieldLengths == null ? null : fieldLengths.clone()); }