JRecord

The Main reason for using JRecord is to read Cobol Data files, but it can be used to

  1. Binary/Text fixed field width files
  2. CSV (Delimited) files

 

JRecord also provides great flexibility in

 

The Normal starting point to JRecord is via the JRecordInterface1 Class. To use JRecordInterface1:
 2             ICobolIOBuilder ioBldr = JRecordInterface1.COBOL
 3                         .newIOBuilder(copybookName)
 4                                 .setDialect( ICopybookDialects.FMT_MAINFRAME)
 5                                 .setFont("cp037")
 6                                 .setFileOrganization(IFileStructureConstants.IO_FIXED_LENGTH)
 7                                 .setDropCopybookNameFromFields(true);

The easiest way to get started with JRecord is to use the Code Generator in the RecordEditor

Important packages in JRecord:

  1. Details - Holds File & line definition classes. Important Interfaces / classes
  2. def.IO.builders - Tis holds the IO Builder definitions, these are what you will be interacting with via claa JRecordInterface1
  3. IO - Record Oriented IO routines. Important classes
  4. External - Classes to read, write and convert File Definitions (Copybooks or Record Layouts).
  5. Type - Classes to convert a field from the External Representation to the Internal format.
  6. ByteIO - Low level IO routines, most user should not need to look at this package

Following is an example of reading & writing a file using IOBuilder classes

 2             ICobolIOBuilder ioBldr = JRecordInterface1.COBOL
 3                         .newIOBuilder(copybookName)
 4                                 .setDialect( ICopybookDialects.FMT_MAINFRAME)
 5                                 .setFont("cp037")
 6                                 .setFileOrganization(IFileStructureConstants.IO_FIXED_LENGTH)
 7                                 .setDropCopybookNameFromFields(true);
 8 
 9             AbstractLineReader reader  = ioBldr.newReader(salesFile);
10             AbstractLineWriter writer  = ioBldr.newWriter(salesFileOut);
11                         
12 
13             while ((saleRecord = reader.read()) != null) {
14                 System.out.print(saleRecord.getFieldValue("KEYCODE-NO").asString()
15                         + " " + saleRecord.getFieldValue("QTY-SOLD").asString()
16                         + " " + saleRecord.getFieldValue("SALE-PRICE").asString());
17 
18                 gstExclusive = saleRecord.getFieldValue("SALE-PRICE").asDouble() / GST_CONVERSION;
19                 saleRecord.getFieldValue("SALE-PRICE").set(gstExclusive);
20                 writer.write(saleRecord);
21 
22                 System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
23             }
24 
25             reader.close();
26             writer.close();