Search code examples
javafilefixed-length-record

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, i.e. files are needed to work with COBOL.

Any suggestions are greatly appreciated!

Thanks.


Solution

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