将平面文件转换为 Java 对象

发布于 2024-08-02 08:53:45 字数 1536 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓦然回首 2024-08-09 08:53:45

快速更新:扁虫已经有一段时间没有活跃了,有一个名为 BeanIO 的分支:
http://www.beanio.org/

Quick update: flatworm has not been active for quite a while, there is a fork named BeanIO:
http://www.beanio.org/

煮茶煮酒煮时光 2024-08-09 08:53:45

FFP - 平面文件解析库
http://jffp.sourceforge.net/

FFP - Flat file parsing library
http://jffp.sourceforge.net/

万水千山粽是情ミ 2024-08-09 08:53:45

我编写的使用 Java 注释的另一种选择是 JFileHelpers - http://jfilehelpers.com

带注释的 bean 示例:

@FixedLengthRecord()
public class Customer {
    @FieldFixedLength(4)
    public Integer custId;

    @FieldAlign(alignMode=AlignMode.Right)
    @FieldFixedLength(20)
    public String name;

    @FieldFixedLength(3)
    public Integer rating;

    @FieldTrim(trimMode=TrimMode.Right)
    @FieldFixedLength(10)
    @FieldConverter(converter = ConverterKind.Date,
        format = "dd-MM-yyyy")
    public Date addedDate;

    @FieldFixedLength(3)
    @FieldOptional
    public String stockSymbol;
}

那么你所要做的就是:

  FileHelperEngine<Customer> engine =
      new FileHelperEngine<Customer>(Customer.class);
  List<Customer> customers =
      new ArrayList<Customer>();

  customers = engine.readResource(
      "/samples/customers-fixed.txt");

Another alternative, that I wrote that uses Java Annotations is JFileHelpers - http://jfilehelpers.com

An example of annotated bean:

@FixedLengthRecord()
public class Customer {
    @FieldFixedLength(4)
    public Integer custId;

    @FieldAlign(alignMode=AlignMode.Right)
    @FieldFixedLength(20)
    public String name;

    @FieldFixedLength(3)
    public Integer rating;

    @FieldTrim(trimMode=TrimMode.Right)
    @FieldFixedLength(10)
    @FieldConverter(converter = ConverterKind.Date,
        format = "dd-MM-yyyy")
    public Date addedDate;

    @FieldFixedLength(3)
    @FieldOptional
    public String stockSymbol;
}

Then all you have to do is:

  FileHelperEngine<Customer> engine =
      new FileHelperEngine<Customer>(Customer.class);
  List<Customer> customers =
      new ArrayList<Customer>();

  customers = engine.readResource(
      "/samples/customers-fixed.txt");
不离久伴 2024-08-09 08:53:45

我没有使用过这个JFlat,但似乎这个框架提供从平面文件到 Java 对象的转换。

类似地 BeanIOJsefa还提供了简单灵活的API。

您可以尝试使用 FlatPack - 但它很旧,而且文档不如 JFlat 或 BeanIO

Apache Camel 有 Flatpack 组件,从 2.10 开始它有 BeanIO 组件

I have not used this JFlat, But it seems this Framework provides conversion from Flat file to Java object.

Similarly BeanIO and Jsefa also provides a simple and flexible API.

You can try with FlatPack - but it is OLD and the docs are not good as JFlat or BeanIO

Apache Camel has Flatpack component as well as from 2.10 it has BeanIO component

镜花水月 2024-08-09 08:53:45

您想考虑JRecordBind(我是它的作者)

与其他人不同,它能够解析和创建平面文件,并且它使用纯 XML 模式(因此您不必学习其他配置语法)。
一些用户回收相同的 XSD 来生成 Web 服务和平面文件输出。

ps:我最近将代码移至 github

You would like to consider JRecordBind (I'm its author)

Unlike others, it's able to both parse and create flat files and it uses plain XML Schema (so you don't have to learn yet another configuration syntax).
Some users recycle the same XSD for producing both webservice and flat files output.

ps: I've recently moved the code to github

妖妓 2024-08-09 08:53:45

您还可以尝试 Fixedformat4j
我喜欢注释方法,并且定义自定义字段格式非常简单。

You can also give a try to Fixedformat4j.
I like the annotations approach and it's very simple to define a custom field format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文