60.2.4. 4.FixedLengthRecord


FixedLengthRecord 注解用于识别模型的根类。它代表记录 = "一个文件/消息行包含数据固定长度(字符数)格式",并可链接到多个子模型类。这个格式是有点的,因为字段的数据可以与右侧或左侧一致。

当数据的大小没有完全填满字段的长度时,我们可以添加"平板"字符。

Expand
注解名称记录类型级别

FixedLengthRecord

已修复

Expand
参数名称类型必填默认值info

countGrapheme

布尔值

 

false

指明如何计算收费

CRLF

字符串

 

WINDOWS

用于在每次记录后添加回车符(可选)。可能的值: WINDOWS、UNIX、MAC 或自定义。这个选项仅在 marshalling 期间使用,除非自定义 eol,否则 unmarshalling 使用系统默认 JDK 提供的行分隔符。

EOL

字符串

  

用来在 unmarshalling 时处理在各个记录后考虑行结尾的字符(可选 - default: "",它有助于使用默认 JDK 提供的行分隔符),除非提供任何其他行分隔符,否则此选项仅在 unmarshalling 期间使用,其中 marshalling 使用系统默认行分隔符作为"WINDOWS",除非提供了任何其他值。

footer

 

void

表示此类型的记录后可以跟随文件末尾的单个页脚记录

header

 

void

表示此类型的记录在文件的开头之间可以加上一个标头记录

ignoreMissingChars

布尔值

 

false

指明是否忽略短行

ignoreTrailingChars

布尔值

 

false

当解压缩 / 解析时,可以忽略超出最后一个映射文件的字符。此注解与模型的根类关联,必须一次性声明。

length

int

 

0

记录的固定长度(字符数)。这意味着记录始终是带有 \{#paddingChar ()} 的较长的 padded。

name

字符串

  

描述记录的名称(可选)

paddingChar

char

  

收费到平板,

skipFooter

布尔值

 

false

配置数据格式以跳过页脚记录的 / unmarshalling。在主记录上配置此参数(例如,而不是标头或页脚)。

skipHeader

布尔值

 

false

配置数据格式以跳过标题记录的 / unmarshalling / unmarshalling。在主记录上配置此参数(例如,而不是标头或页脚)。

记录可能不是标头/页脚和主固定长度记录。

问题单 1:简单固定长度记录

这个简单示例演示了如何设计模型来解析/格式化固定消息

10A9PaulineMISINXD12345678BUYShare2500.45USD01-08-2009
Copy to Clipboard Toggle word wrap

fixed-simple

@FixedLengthRecord(length=54, paddingChar=' ')
public static class Order {

    @DataField(pos = 1, length=2)
    private int orderNr;

    @DataField(pos = 3, length=2)
    private String clientNr;

    @DataField(pos = 5, length=7)
    private String firstName;

    @DataField(pos = 12, length=1, align="L")
    private String lastName;

    @DataField(pos = 13, length=4)
    private String instrumentCode;

    @DataField(pos = 17, length=10)
    private String instrumentNumber;

    @DataField(pos = 27, length=3)
    private String orderType;

    @DataField(pos = 30, length=5)
    private String instrumentType;

    @DataField(pos = 35, precision = 2, length=7)
    private BigDecimal amount;

    @DataField(pos = 42, length=3)
    private String currency;

    @DataField(pos = 45, length=10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}
Copy to Clipboard Toggle word wrap

问题单 2:修复带有校准和 padding 的长度记录

这更高级的示例演示了如何为字段定义对齐以及如何分配"这里 "的 padding 字符:

10A9 PaulineM ISINXD12345678BUYShare2500.45USD01-08-2009
Copy to Clipboard Toggle word wrap

fixed-padding-align

@FixedLengthRecord(length=60, paddingChar=' ')
public static class Order {

    @DataField(pos = 1, length=2)
    private int orderNr;

    @DataField(pos = 3, length=2)
    private String clientNr;

    @DataField(pos = 5, length=9)
    private String firstName;

    @DataField(pos = 14, length=5, align="L")   // align text to the LEFT zone of the block
    private String lastName;

    @DataField(pos = 19, length=4)
    private String instrumentCode;

    @DataField(pos = 23, length=10)
    private String instrumentNumber;

    @DataField(pos = 33, length=3)
    private String orderType;

    @DataField(pos = 36, length=5)
    private String instrumentType;

    @DataField(pos = 41, precision = 2, length=7)
    private BigDecimal amount;

    @DataField(pos = 48, length=3)
    private String currency;

    @DataField(pos = 51, length=10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}
Copy to Clipboard Toggle word wrap

问题单 3: Field padding

有时,为记录定义的默认 padding 不能应用于字段,因为我们有一个数字格式,我们想使用 '0' 而不是 ' '。在这种情况下,您可以在模型中使用 @DataField 属性 paddingChar,以设置此值。

10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009
Copy to Clipboard Toggle word wrap

fixed-padding-field

@FixedLengthRecord(length = 65, paddingChar = ' ')
public static class Order {

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 3, length = 2)
    private String clientNr;

    @DataField(pos = 5, length = 9)
    private String firstName;

    @DataField(pos = 14, length = 5, align = "L")
    private String lastName;

    @DataField(pos = 19, length = 4)
    private String instrumentCode;

    @DataField(pos = 23, length = 10)
    private String instrumentNumber;

    @DataField(pos = 33, length = 3)
    private String orderType;

    @DataField(pos = 36, length = 5)
    private String instrumentType;

    @DataField(pos = 41, precision = 2, length = 12, paddingChar = '0')
    private BigDecimal amount;

    @DataField(pos = 53, length = 3)
    private String currency;

    @DataField(pos = 56, length = 10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}
Copy to Clipboard Toggle word wrap

问题单 4:带有分隔符的固定长度记录

固定长度记录有时在记录中有分隔的内容。firstName 和 lastName 字段在以下示例中用 ^ 字符分隔:

10A9Pauline^M^ISINXD12345678BUYShare000002500.45USD01-08-2009
Copy to Clipboard Toggle word wrap

fixed- separated

@FixedLengthRecord
public static class Order {

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 2, length = 2)
    private String clientNr;

    @DataField(pos = 3, delimiter = "^")
    private String firstName;

    @DataField(pos = 4, delimiter = "^")
    private String lastName;

    @DataField(pos = 5, length = 4)
    private String instrumentCode;

    @DataField(pos = 6, length = 10)
    private String instrumentNumber;

    @DataField(pos = 7, length = 3)
    private String orderType;

    @DataField(pos = 8, length = 5)
    private String instrumentType;

    @DataField(pos = 9, precision = 2, length = 12, paddingChar = '0')
    private BigDecimal amount;

    @DataField(pos = 10, length = 3)
    private String currency;

    @DataField(pos = 11, length = 10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}
Copy to Clipboard Toggle word wrap

可以使用 ordinal、sequential 值而不是精确的列号来定义固定长度记录中的 pos 值。

案例 5:修复长度的长度记录,记录定义的字段长度

有时,固定长度记录可能包含一个字段,用来定义同一记录中另一个字段的预期长度。在以下示例中,instrumentNumber 字段值的长度由记录中的 instrumentNumberLen 字段的值定义。

10A9Pauline^M^ISIN10XD12345678BUYShare000002500.45USD01-08-2009
Copy to Clipboard Toggle word wrap

fixed- separated

@FixedLengthRecord
public static class Order {

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 2, length = 2)
    private String clientNr;

    @DataField(pos = 3, delimiter = "^")
    private String firstName;

    @DataField(pos = 4, delimiter = "^")
    private String lastName;

    @DataField(pos = 5, length = 4)
    private String instrumentCode;

    @DataField(pos = 6, length = 2, align = "R", paddingChar = '0')
    private int instrumentNumberLen;

    @DataField(pos = 7, lengthPos=6)
    private String instrumentNumber;

    @DataField(pos = 8, length = 3)
    private String orderType;

    @DataField(pos = 9, length = 5)
    private String instrumentType;

    @DataField(pos = 10, precision = 2, length = 12, paddingChar = '0')
    private BigDecimal amount;

    @DataField(pos = 11, length = 3)
    private String currency;

    @DataField(pos = 12, length = 10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}
Copy to Clipboard Toggle word wrap

问题单 6:使用标头和页脚修复长度记录

Bindy 将发现配置为模型一部分的固定长度标头和页脚记录 - 如果注解的类与主 @FixedLengthRecord 类相同,或者在配置的扫描软件包之一内存在。以下文本演示了一个标题记录和页脚记录所括号的两个固定长度记录。

101-08-2009
10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009
10A9 RichN ISINXD12345678BUYShare000002700.45USD01-08-2009
9000000002
Copy to Clipboard Toggle word wrap

fixed-header-and-footer-main-class

@FixedLengthRecord(header = OrderHeader.class, footer = OrderFooter.class)
public class Order {

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 2, length = 2)
    private String clientNr;

    @DataField(pos = 3, length = 9)
    private String firstName;

    @DataField(pos = 4, length = 5, align = "L")
    private String lastName;

    @DataField(pos = 5, length = 4)
    private String instrumentCode;

    @DataField(pos = 6, length = 10)
    private String instrumentNumber;

    @DataField(pos = 7, length = 3)
    private String orderType;

    @DataField(pos = 8, length = 5)
    private String instrumentType;

    @DataField(pos = 9, precision = 2, length = 12, paddingChar = '0')
    private BigDecimal amount;

    @DataField(pos = 10, length = 3)
    private String currency;

    @DataField(pos = 11, length = 10, pattern = "dd-MM-yyyy")
    private Date orderDate;
}

@FixedLengthRecord
public  class OrderHeader {
    @DataField(pos = 1, length = 1)
    private int recordType = 1;

    @DataField(pos = 2, length = 10, pattern = "dd-MM-yyyy")
    private Date recordDate;
}

@FixedLengthRecord
public class OrderFooter {

    @DataField(pos = 1, length = 1)
    private int recordType = 9;

    @DataField(pos = 2, length = 9, align = "R", paddingChar = '0')
    private int numberOfRecordsInTheFile;
}
Copy to Clipboard Toggle word wrap

case 7:在解析固定长度记录时跳过内容

通常,与提供固定长度记录的系统集成,其中包括超过目标用例所需的信息。在这种情况下,可以跳过我们不需要的这些字段的声明和解析。为保证这一点,如果下一个声明的字段的 pos 值超过最后一次解析字段的光标位置,Bindy 将跳过到记录中的下一个映射字段。对感兴趣的字段使用绝对位置(而不是 ordinal 值)会导致 Bindy 在两个字段之间跳过内容。

同样,除某些字段以外的任何内容都不是感兴趣的。在这种情况下,您可以通过在 @FixedLengthRecord 声明上设置 ignoreTrailingChars 属性,告知 Bindy 跳过最后一个映射字段以外的解析。

@FixedLengthRecord(ignoreTrailingChars = true)
public static class Order {

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 3, length = 2)
    private String clientNr;

    // any characters that appear beyond the last mapped field will be ignored

}
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat