在 Java 中解析固定宽度的格式化文件 [英] Parsing a fixed-width formatted file in 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_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 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屋!