Java fixed-width file format read/write library
Asked Answered
V

5

7

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

Any suggestions are greatly appreciated!

Thanks.

Vesta answered 29/12, 2011 at 15:49 Comment(2)
also asked here: #7482521Continuation
I ended up using BeanIO but thanks for help! Got me pointed in the right direction.Vesta
H
4

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 ...).

Husha answered 29/12, 2011 at 16:2 Comment(0)
C
6

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).

Congou answered 23/11, 2014 at 7:37 Comment(2)
That is a very good parser, but it doesn't allow you to go from the internal java structure to the fixed-width file, i.e. it is only a reader, not a writer.Modifier
@alianos It seems you didn't try the FixedWidthWriter. Check some examples here: github.com/uniVocity/univocity-parsers/blob/master/src/test/…Congou
H
4

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 ...).

Husha answered 29/12, 2011 at 16:2 Comment(0)
B
3

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

It is the exact purpose of this library

Bellflower answered 15/8, 2013 at 9:19 Comment(0)
P
1

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
Priedieu answered 15/2, 2012 at 21:12 Comment(0)
Z
1

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.
Zaid answered 5/8, 2014 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.