简体   繁体   中英

Java fixed-width file format read/write library

I'm looking for a good Java library that easily allows the read/write of fixed-width files. Needed for maintaining legacy systems, ie files are needed to work with COBOL.

Any suggestions are greatly appreciated!

Thanks.

uniVocity-parsers parses/writes Fixed-Width inputs (as well as CSV and TSV). It has quite a lot of features you which could use.

Sample input:

YearMake_Model___________________________________Description_____________________________Price___
1997Ford_E350____________________________________ac, abs, moon___________________________3000.00_
1999ChevyVenture "Extended Edition"______________________________________________________4900.00_
1996Jeep_Grand Cherokee__________________________MUST SELL!
air, moon roof, loaded_______4799.00_
1999ChevyVenture "Extended Edition, Very Large"__________________________________________5000.00_
_________Venture "Extended Edition"______________________________________________________4900.00_

Code to read:

FixedWidthFieldLengths lengths = new FixedWidthFieldLengths(4, 5, 40, 40, 8);
FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths);
//sets the character used for padding unwritten spaces in the file
settings.getFormat().setPadding('_');

// creates a fixed-width parser with the given settings
FixedWidthParser parser = new FixedWidthParser(settings);
// parses all rows in one go.
List<String[]> allRows = parser.parseAll(new FileReader(yourFile));

Output:

[Year, Make, Model, Description, Price]
[1997, Ford, E350, ac, abs, moon, 3000.00]
[1999, Chevy, Venture "Extended Edition", null, 4900.00]
[1996, Jeep, Grand Cherokee, MUST SELL!
air, moon roof, loaded, 4799.00]
[1999, Chevy, Venture "Extended Edition, Very Large", null, 5000.00]
[null, null, Venture "Extended Edition", null, 4900.00]

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

You could also take a look at Fixedformat4j: http://fixedformat4j.ancientprogramming.com/

It is the exact purpose of this library

I would use ByteBuffer, possibly with memory mapped files. This allows to read/write primitive type in big or little endian. This option is best for fixed width binary data.

For fixed width text you can use BufferedReader.readLine() and String.substring(from, to) to get the fields you want. To output fixed width fields you can use PrintWriter.printf(format, fields ...) .

A schema based approach:

  • JSaPar Allows you to specify a schema by which you can parse or generate fixed width text. Also does some basic type checking and type conversions.
  • There is also a list of other libraries mentioned here that could be of help to you.

You could look at

  • JRecord Library to read/write cobol files from Java, supports a variety of Cobol Dialects and formats
  • cb2java Read Cobol files
  • Legstar library to proces cobol ata
  • cb2xml Converts Cobol files to Xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM