在Java中解析固定宽度格式文件的最佳方法是什么? [英] What's the best way of parsing a fixed-width formatted file in Java?

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

问题描述

我有一个来自供应商的文件,每行有115个固定宽度的字段。将该文件解析为115个字段的最佳方法是什么,所以我可以在我的代码中使用它们?

I've got a file from a vendor that has 115 fixed-width fields per line. What's the best way of parsing that file into the 115 fields so I can use them in my code?

我的第一个想法是为每个字段制作常量,如 NAME_START_POSITION NAME_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 if there's any other recommended ways of doing this. None of the couple of libraries a Google search turned up seemed any better either. Thanks

推荐答案

我会使用像扁虫而不是重新发明轮子:它有一个干净的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天全站免登陆