用Java解析固定宽度格式的文件 [英] Parsing a fixed-width formatted file in Java

查看:89
本文介绍了用Java解析固定宽度格式的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从供应商那里得到了一个文件,每行有115个固定宽度的字段.如何将文件解析为115个字段,以便可以在代码中使用它们?

I've got a file from a vendor that has 115 fixed-width fields per line. How can I parse that file into the 115 fields so I can use them in my code?

我的第一个想法只是为每个字段(例如NAME_START_POSITIONNAME_LENGTH)创建常量,并使用substring.这看起来很难看,所以我很好奇这样做的更好方法.谷歌搜索打开的两个库中,似乎也没有一个好.

My first thought is just to make constants for each field like NAME_START_POSITION and NAME_LENGTH and using substring. That just seems ugly, so I'm curious about better ways of doing this. None of the couple of libraries a Google search turned up seemed any better, either.

推荐答案

我将使用诸如 flatworm的平面文件解析器而不是重新发明轮子:它具有简洁的API,易于使用,具有良好的错误处理和简单的文件格式描述符.另一种选择是 jFFP ,但我更喜欢第一个.

I would use a flat file parser like flatworm instead of reinventing the wheel: it has a clean API, is simple to use, has decent error handling and a simple file format descriptor. Another option is jFFP but I prefer the first one.

这篇关于用Java解析固定宽度格式的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆