Prev Up Next

Reading and Writing Files

The package provides standard classes for reading, writing Fixed formatting data files


Reading a File

The example program XmplLineIO1 provides a simple example of processing a file using the RecordEditor's IO routines.

 14:package net.sf.RecordEditor.examples;
 15:
 16:import net.sf.RecordEditor.record.LayoutDetail;
 17:import net.sf.RecordEditor.record.Line;
 18:import net.sf.RecordEditor.record.RecordDetail;
 19:import net.sf.RecordEditor.record.io.TextLineReader;
 20:import net.sf.RecordEditor.utils.CopyBookDbReader;
 21:
 22:/**
 23: * This Example program demonstrates Reading a file using Line Based
 24: * Routines
 25: *
 26: * @author Bruce Martin
 27: *
 28: */
 29:public final class XmplLineIO1 {
 30:
 31:    private String poFile           = Constants.RECORDEDIT_INSTALL_DIRECTORY
 32:                                                                + "SampleFiles\\Ams_PODownload_20050101.txt";
 33:
 34:    private CopyBookDbReader copybook
 35:                                    = new CopyBookDbReader();
 36:    private LayoutDetail copyBookPO  = copybook.getLayout("ams PO Download");
 37:
 38:    private int poRecordId          = copyBookPO.getRecordIndex("ams PO Download: Header");
 39:    private RecordDetail poRecord   = copyBookPO.getRecord(poRecordId);
 40:
 41:    private int fieldPO             = poRecord.getFieldIndex("PO");
 42:    private int fieldVendor         = poRecord.getFieldIndex("Vendor");
 43:    private int fieldDepartment     = poRecord.getFieldIndex("Department");
 44:    private int fieldDepartmentName = poRecord.getFieldIndex("Department Name");
 45:    private int fieldRecieptDate    = poRecord.getFieldIndex("Expected Reciept Date");
 46:    private int fieldOrderType      = poRecord.getFieldIndex("Order Type");
 47:
 48:    private TextLineReader reader   = new TextLineReader();
 49:
 50:    private Line line;
 51:
 52:    /**
 53:     *
 54:     */
 55:    private XmplLineIO1() {
 56:        super();
 57:
 58:        int lineNumber = 0;
 59:
 60:        try {
 61:            System.out.println("     Department        PO  Type  Date  Vendor");
 62:            System.out.println("  ===========================================");
 63:
 64:            reader.open(poFile, copyBookPO);
 65:
 66:            while ((line = reader.read()) != null) {
 67:                lineNumber += 1;
 68:                if (line.getPreferredLayoutIdx() == poRecordId) {
 69:                    System.out.println(
 70:                            "  " + line.getField(poRecordId, fieldDepartment)
 71:                          + "  " + line.getField(poRecordId, fieldDepartmentName)
 72:                          + "  " + line.getField(poRecordId, fieldPO)
 73:                          + "  " + line.getField(poRecordId, fieldOrderType)
 74:                          + "  " + line.getField(poRecordId, fieldRecieptDate)
 75:                          + "  " + line.getField(poRecordId, fieldVendor)
 76:                    );
 77:                }
 78:            }
 79:
 80:            reader.close();
 81:
 82:        } catch (Exception e) {
 83:            System.out.println("Error Line " + lineNumber + " " + e.getMessage());
 84:            System.out.println();
 85:            System.out.println();
 86:            e.printStackTrace();
 87:        }
 88:
 89:        System.out.println();
 90:        System.out.println("Lines Read " + lineNumber + " PO Field " + fieldPO);
 91:    }
 92:
 93:
 94:    /**
 95:     * LineIO example
 96:     *
 97:     * @param args program arguments
 98:     */
 99:    public static void main(String[] args) {
100:        new XmplLineIO1();
101:    }
102:}


Reading using User-Defined-Lines

The example program XmplLineIO2 illustrates the following points:

   1:    private CopyBookDbReader copybook  = new CopyBookDbReader();
   2:    private LayoutDetail salesCopyBook = copybook.getLayout("DTAR020");
   3:    private int         fileStructure  = salesCopyBook.getFileStructure();
   4:
   5:    private LineIOProvider ioProvider  = new LineIOProvider();
   6:
   7:    private AbsLineReader reader       = ioProvider.getLineReader(fileStructure,
   8:                                                                  new DTAR0020provider());
   9:    private AbsLineWriter writer       = ioProvider.getLineWriter(fileStructure);
  10:


Generating Interfaces to files

The XmplLineBuilder can be used to generate interfaces classes to Cobol like files.

The main method is genType. It has the following parameters:
typeNametype name to be generated
extendProcesswhat class the generated process class should extend
arrayslist of arrays to watch for
getAndSetwether to generate getters and setters
defineFieldswether to define all the fields

This method builds 3 classes:

LineThe program builds a Line class specifically for the supplied Record Layout with optional Getter's and Setter's.
LineProviderA line provider for the Generated Line class
ProcessA class to process files of the supplied Record Layout.

   1:    public static void main(String[] args) {
   2:        XmplLineBuilder builder = new XmplLineBuilder();
   3:        String[] amsPoArrayFields = {"DC Number", "Pack Quantity"};
   4:        String[] ediPoArrayFields = {"Store Num", "Ord Qty"};
   5:
   6:        builder.genType("ams PO Download", "", amsPoArrayFields, true, false);
   7:        builder.genType("ams Receipt", "", XmplLineBuilder.NULL_ARRAYS, false, false);
   8:        builder.genType("EDI PO", "", ediPoArrayFields, true, false);
   9:    }
  10:

There are examples of generated java programs in package net.sf.RecordEditor.examples.genCode


Record Layout Generate Line Generated Line Provider Generated Process Class
ams PO Download AmsPoDownload AmsPoDownloadProvider AmsPoDownloadProcess
ams Receipt AmsReceipt AmsReceiptProvider AmsReceiptProcess
EDI PO EdiPo EdiPoProvider EdiPoProcess

Prev Up Next