BeanIO Commands

Published: 2018-01-20, Updated: 2020-03-20

Examplos

CSV Exporter

public PedreiroExporter() {
	final RecordParserFactory parserFactory = new CsvParserBuilder()
		.delimiter(',')
		.recordTerminator("\r\n")
		.build()
		.getInstance();

	final StreamBuilder builder = new StreamBuilder(getClass().getName())
		.format("csv")
		.parser(parserFactory)
		.addTypeHandler(ListHandler.class.getName(), new ListHandler())
		.addRecord(PedreiroHeaderCSV.class)
		.addRecord(PedreiroCSV.class);

	factory = StreamFactory.newInstance();
	factory.define(builder);
}

public BeanWriter createWriter(Writer w){
	return this.factory.createWriter(getClass().getName(), w);
}

Criando um handler


public class ListHandler implements TypeHandler {
	@Override
	public Object parse(String text) throws TypeConversionException {
		throw new UnsupportedOperationException();
	}

	@Override
	public String format(Object value) {
		if(value == null){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (Object o : ((Collection) value)) {
			sb.append(o);
			sb.append(", ");
		}
		if(sb.length() > 0){
			sb.delete(sb.length() - 2, sb.length());
		}
		return sb.toString();
	}

	@Override
	public Class<?> getType() {
		return List.class;
	}
}

Gerando com zeros campos inteiros que sao nulos

Por default o beanio vai gerar o campo com espaços mesmo que voce especifique o padding 0 se ele nao for primitivo, eg. Integer, Long; para gerar com zeros mesmo assim ha duas opcoes

@Field(required = true)
@Field(defaultValue = true)
final StreamFactory factory = StreamFactory.newInstance();
final FixedLengthParserBuilder parser = new FixedLengthParserBuilder().recordTerminator("\n");
final StreamBuilder builder = new StreamBuilder("myname").format("fixedlength")
	.parser(parser)
	.addRecord(MyBean.class);
factory.define(builder);
final StringWriter out = new StringWriter();
final BeanWriter writer = factory.createWriter("myname", out);
writer.write(new MyBean());
writer.flush();

System.out.println(out.toString());


@Record
public class MyBean {

	@Field(align = Align.RIGHT, padding = '0', length = 10, required = true)
	private Integer id;
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
}

Keywords

beanio bean io


GTA San Andreas Bookmarks Como escolher o melhor mouse para a sua mão - Logitech G Pro vs G403 vs G502 vs Deathader Elite

Comments