Spring-Batch:将对象写入固定长度的行吗? [英] Spring-Batch: Writing objects to lines with fixed length?
问题描述
Spring-Batch提供了类FixedLengthTokenizer
,该类可以轻松地将单行的不同偏移量读取到对象的文件中.从而从固定长度的某些范围中提取每个字段的内容:
Spring-Batch provides the class FixedLengthTokenizer
which makes it easy to read different offsets of a single line into the fileds of an object. Whereby the content of each field is extracted from certain ranges with fixed length:
FixedLengthTokenizer tokenizer = new FixedLengthTokenizer();
String[] names = {"A", "B", "C", "D"};
tokenizer.setNames(names);
Range[] ranges = {new Range(1, 4), new Range(5, 12), new Range(13, 14), new Range(15, 15)};
tokenizer.setColumns(ranges);
我想完全相反.我想将一个对象写入平面文件,从而将不同的字段以固定的长度写入文件. Spring-Batch提供了接口org.springframework.batch.item.file.transform.LineAggregator
,用于将线映射到对象.但是我想知道为什么没有FixedLengthLineAggregator
这样做吗?
I want to do the exact oposite. I want to write an Object into a flat file whereby the different fields should be written into the file with fixed lengths. Spring-Batch provides the interface org.springframework.batch.item.file.transform.LineAggregator
for mapping lines to objects. But I'm wondering why there is no FixedLengthLineAggregator
for doing that?
在Spring-Batch中,将对象写入行以使字段具有固定长度的正确方法是什么?
What's the right way in Spring-Batch to write objects to lines whereby the fields have a fixed length?
推荐答案
正如@canillas在其评论中指出的那样,管理此问题的方法将是通过Spring Batch的FormatterLineAggregator
.使用String.format
的格式功能,可以使您以独立于JVM和独立于框架的标准方式生成固定长度的记录.
As @canillas noted in their comment, the way to manage this would be via Spring Batch's FormatterLineAggregator
. Using the format capabilities of String.format
can allow you to generate fixed length records in a way that is standard across the JVM and framework independent.
这篇关于Spring-Batch:将对象写入固定长度的行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!