SourceForge.net Logo   JRecord

Java Examples
 

Java Examples

  • Reading a File (ByteReader)
  • Reading a Cobol File
  • Reading and Wrinting a Mainframe Cobol file
  • Reading and Writing a CSV file
  • Reading and Writing using Record-Editor XML Layout
  • Writing a File (XML Layout)
  • Reading a XML file
  • Reading a File (ByteReader)

    This example shows how a ByteReader is used to read a Fujitsu-Cobol variable length file as a array of Bytes. ByteReaders are the lowest level of the IO Tree, they read a line for a file as an array of Bytes. They provide an interface to legacy files.

    Note: Each line in these files consist of

       1:        AbstractByteReader tReader = new FujitsuVbByteReader();
       2:        byte[] line;
       3:
       4:        try {
       5:            tReader.open(fileName);
       6:
       7:            while ((line = tReader.read()) != null) {
       8:                 // TODO Do Something .....
       9:            }
      10:        } finally {
      11:            tReader.close();
      12:        }
      13:
    

    Reading a Cobol File

    This Example shows how to read a Windows / Linux file and access the fields via the Cobol names.

    Cobol RecordLayout

       5:        01 Ams-Vendor.
       6:           03 Brand               Pic x(3).
       7:           03 Vendor-Number       Pic 9(8).
       8:           03 Vendor-Name         Pic X(40).
       9:           03 Filler-1            Pic X(15).
      10:           03 Code-850            Pic 999.
      11:           03 Value-P             Pic X.
      12:           03 Filler              Pic X(3).
      13:           03 Zero-Value          Pic 999.
    

    Java Code to read the file

       1:        String vendorFile          = TstConstants.SAMPLE_DIRECTORY
       2:                                           + "Ams_VendorDownload_20041229.txt";
       3:        String copybookName        = TstConstants.COBOL_DIRECTORY
       4:                                           + "AmsVendor.cbl";
       5:        CobolIoProvider ioProvider = CobolIoProvider.getInstance();
       6:        AbstractLine line;
       7:        int lineNumber = 0;
       8:
       9:        try {
      10:            AbstractLineReader reader  = ioProvider.getLineReader(
      11:                    Constants.IO_TEXT_LINE, Convert.FMT_INTEL,
      12:                    CopybookLoader.SPLIT_NONE, copybookName, vendorFile
      13:            );
      14:
      15:            System.out.println("  Vendor \t Name");
      16:            System.out.println("  ===========================================");
      17:
      18:
      19:            while ((line = reader.read()) != null) {
      20:                lineNumber += 1;
      21:                System.out.println(
      22:                            "  "    + line.getFieldValue("Vendor-Number").asString()
      23:                          + "  \t " + line.getFieldValue("Vendor-Name").asString());
      24:
      25:            }
      26:
      27:            reader.close();
      28:
      29:        } catch (Exception e) {
      30:            System.out.println("Error Line " + lineNumber + " " + e.getMessage());
      31:
    

    Reading and Wrinting a Mainframe Cobol file

    This is how you can read and write a Mainframe Cobol Fixed-Record-length (RECFM=FB) Binary file.

    Cobol Copybook

       6:000600*
       7:000700*   RECORD LENGTH IS 27.
       8:000800*
       9:000900        03  DTAR020-KCODE-STORE-KEY.
      10:001000            05 DTAR020-KEYCODE-NO      PIC X(08).
      11:001100            05 DTAR020-STORE-NO        PIC S9(03)   COMP-3.
      12:001200        03  DTAR020-DATE               PIC S9(07)   COMP-3.
      13:001300        03  DTAR020-DEPT-NO            PIC S9(03)   COMP-3.
      14:001400        03  DTAR020-QTY-SOLD           PIC S9(9)    COMP-3.
      15:001500        03  DTAR020-SALE-PRICE         PIC S9(9)V99 COMP-3.
      16:
    

    Java code to read / write a file

       1:            private static final double GST_CONVERSION = 1.1;
       2:
       3:            private String installDir     = TstConstants.SAMPLE_DIRECTORY;
       4:            private String salesFile      = installDir + "DTAR020.bin";
       5:            private String salesFileOut   = installDir + "DTAR020out.bin";
       6:            private String copybookName   = TstConstants.COBOL_DIRECTORY + "DTAR020.cbl";
       7:
       8:            /**
       9:             * Example of LineReader / LineWrite classes
      10:             */
      11:            private XmplLineIO6() {
      12:                super();
      13:
      14:                int lineNum = 0;
      15:                double gstExclusive;
      16:                AbstractLine saleRecord;
      17:
      18:                try {
      19:                    int fileStructure = Constants.IO_FIXED_LENGTH;
      20:                    CobolIoProvider ioProvider = CobolIoProvider.getInstance();
      21:                    AbstractLineReader reader  = ioProvider.getLineReader(
      22:                           fileStructure, Convert.FMT_MAINFRAME,
      23:                            CopybookLoader.SPLIT_NONE, copybookName, salesFile
      24:                    );
      25:
      26:                    AbstractLineWriter writer  = ioProvider.getLineWriter(fileStructure, salesFileOut);
      27:
      28:                    while ((saleRecord = reader.read()) != null) {
      29:                        lineNum += 1;
      30:
      31:                        System.out.print(saleRecord.getFieldValue("KEYCODE-NO").asString()
      32:                                + " " + saleRecord.getFieldValue("QTY-SOLD").asString()
      33:                                + " " + saleRecord.getFieldValue("SALE-PRICE").asString());
      34:
      35:                        gstExclusive = saleRecord.getFieldValue("SALE-PRICE").asDouble() / GST_CONVERSION;
      36:                        saleRecord.getFieldValue("SALE-PRICE").set(gstExclusive);
      37:                        writer.write(saleRecord);
      38:
      39:                        System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
      40:                    }
      41:
      42:                    reader.close();
      43:                    writer.close();
      44:                } catch (Exception e) {
      45:                    System.out.println("~~> " + lineNum + " " + e.getMessage());
      46:                    System.out.println();
      47:
      48:                    e.printStackTrace();
      49:                }
      50:
    

    Reading and Writing a CSV file

    The following example shows you how to read and write a CSV file with the field names on the first line and a field-seperator of ":"

    14:            private static final double GST_CONVERSION = 1.1;
    15:
    16:            private String installDir          = TstConstants.SAMPLE_DIRECTORY;
    17:            private String salesFile           = installDir + "DTAR020.csv";
    18:            private String salesFileOut        = installDir + "DTAR020out.csv";
    19:
    20:            /**
    21:             * Example of LineReader / LineWrite classes
    22:             */
    23:            private () {
    24:                super();
    25:
    26:                int lineNum = 0;
    27:                double gstExclusive;
    28:                AbstractLine saleRecord;
    29:
    30:                try {
    31:
    32:                    TextLineReader reader  = new TextLineReader(null, true);
    33:
    34:                    TextLineWriter writer  = new TextLineWriter(true);
    35:
    36:                    reader.setDefaultDelim(":");
    37:                    reader.open(salesFile);
    38:                    writer.open(salesFileOut);
    39:
    40:                    while ((saleRecord = reader.read()) != null) {
    41:                        lineNum += 1;
    42:
    43:                        System.out.print(saleRecord.getField("KEYCODE_NO")
    44:                                + " " + saleRecord.getField("QTY_SOLD")
    45:                                + " " + saleRecord.getField("SALE_PRICE"));
    46:
    47:                        gstExclusive = Double.valueOf(saleRecord.getField("SALE_PRICE").toString()) / GST_CONVERSION;
    48:                        saleRecord.setField("SALE_PRICE", Double.valueOf(gstExclusive));
    49:                        writer.write(saleRecord);
    50:
    51:                        System.out.println(" " + saleRecord.getField("SALE_PRICE"));
    52:                    }
    53:
    54:                    reader.close();
    55:                    writer.close();
    56:                } catch (Exception e) {
    57:                    System.out.println("~~> " + lineNum + " " + e.getMessage());
    58:                    System.out.println();
    59:
    60:                    e.printStackTrace();
    61:                }
    62:            }
    

    Reading and Writing using Record-Editor XML Layout

    Instead of using Cobool Copybooks, you can use the XML to describe the layout. Here is an example of reading / writing a file using a Record-Editor XML layout (or copybook).

    RecordLayout for a mainframe file file:

       1:<?xml version="1.0" ?>
       2:<RECORD RECORDNAME="DTAR020" COPYBOOK="DTAR020" DELIMITER="&lt;Tab&gt;" 
       3:        FONTNAME="CP037" FILESTRUCTURE="Default" STYLE="0" RECORDTYPE="RecordLayout"
       4:        LIST="Y" QUOTE="" RecSep="default">
       5:        <FIELDS>
       6:                <FIELD NAME="KEYCODE-NO" POSITION="1"  LENGTH="8" TYPE="Char" />
       7:                <FIELD NAME="STORE-NO"   POSITION="9"  LENGTH="2" TYPE="Mainframe Packed Decimal (comp-3)" />
       8:                <FIELD NAME="DATE"       POSITION="11" LENGTH="4" TYPE="Mainframe Packed Decimal (comp-3)" />
       9:                <FIELD NAME="DEPT-NO"    POSITION="15" LENGTH="2" TYPE="Mainframe Packed Decimal (comp-3)" />
      10:                <FIELD NAME="QTY-SOLD"   POSITION="17" LENGTH="5" TYPE="Mainframe Packed Decimal (comp-3)" />
      11:                <FIELD NAME="SALE-PRICE" POSITION="22" LENGTH="6" DECIMAL="2" TYPE="Mainframe Packed Decimal (comp-3)" />
      12:        </FIELDS>
      13:</RECORD>
    

    Java Code to read the file:

       1:                double GST_CONVERSION = 1.1;
       2:
       3:                String installDir     = TstConstants.SAMPLE_DIRECTORY;
       4:                String salesFile      = installDir + "DTAR020.bin";
       5:                String salesFileOut   = installDir + "DTAR020out.bin";
       6:                String copybookName   = TstConstants.RECORD_EDITOR_XML_DIRECTORY
       7:                                               + "DTAR020.Xml";
       8:                int lineNum = 0;
       9:                double gstExclusive;
      10:                AbstractLine saleRecord;
      11:
      12:                try {
      13:                    CopybookLoader loader = new RecordEditorXmlLoader();
      14:                    LayoutDetail layout = loader.loadCopyBook(copybookName, 0, 0, "", 0, 0, null).asLayoutDetail();
      15:
      16:                    /* with XML copybooks, get the file structure from layout */
      17:                    int fileStructure = layout.getFileStructure();
      18:
      19:                    AbstractLineReader reader  = LineIOProvider.getInstance().getLineReader(fileStructure);
      20:                    AbstractLineWriter writer  = LineIOProvider.getInstance().getLineWriter(fileStructure);
      21:
      22:                    reader.open(salesFile, layout);
      23:                    writer.open(salesFileOut);
      24:
      25:                    while ((saleRecord = reader.read()) != null) {
      26:                        lineNum += 1;
      27:
      28:                        System.out.print(saleRecord.getFieldValue("KEYCODE-NO").asString()
      29:                                + " " + saleRecord.getFieldValue("QTY-SOLD").asString()
      30:                                + " " + saleRecord.getFieldValue("SALE-PRICE").asString());
      31:
      32:                        gstExclusive = saleRecord.getFieldValue("SALE-PRICE").asDouble() / GST_CONVERSION;
      33:                        saleRecord.getFieldValue("SALE-PRICE").set(gstExclusive);
      34:                        writer.write(saleRecord);
      35:
      36:                        System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
      37:                    }
      38:
      39:                    reader.close();
      40:                    writer.close();
      41:                } catch (Exception e) {
      42:
    

    Writing a File (XML Layout)

    Here is an example of how to create some data then write it to a File.

    Note: Layout Definition is in held in a RecordEditor-Xml format.

       1:                    int fileStructure = Constants.IO_FIXED_LENGTH;
       2:                    CopybookLoader loader = new RecordEditorXmlLoader();
       3:                    ExternalRecord extlayout = loader.loadCopyBook(copybookName, 0, 0, "", 0, 0, null);
       4:                    /* 
       5:                     * Converting from the interchange (ExternalRecord) format to LayoutDetail
       6:                     */
       7:                    LayoutDetail layout = ToLayoutDetail.getInstance().getLayout(extlayout);
       8:                    AbstractLine saleRecord = new Line(layout);
       9:                    AbstractLineWriter writer  = LineIOProvider.getInstance().getLineWriter(fileStructure);
      10:
      11:                    writer.open(salesFileOut);
      12:
      13:                    saleRecord.getFieldValue("KEYCODE-NO").set(1331);
      14:                    saleRecord.getFieldValue("STORE-NO").set(1);
      15:                    saleRecord.getFieldValue("DATE").set(80921);
      16:                    saleRecord.getFieldValue("DEPT-NO").set(100);
      17:                    saleRecord.getFieldValue("QTY-SOLD").set(7);
      18:                    saleRecord.getFieldValue("SALE-PRICE").set(7.00);
      19:                    writer.write(saleRecord);
      20:
      21:                    saleRecord.getFieldValue("STORE-NO").set(11);
      22:                    writer.write(saleRecord);
      23:
      24:                    saleRecord.getFieldValue("STORE-NO").set(11);
      25:                    writer.write(saleRecord);
      26:
      27:                    System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
      28:                    writer.close();
      29:
    

    Reading a XML file

    If you really want to; you can also read XML files in much the same way. You need Java Version 6 or a StAX package installed.

       1:        AbstractLine line;
       2:        XmlLineReader reader = new XmlLineReader(true);
       3:        String field, id;
       4:
       5:
       6:        try {
       7:            reader.open(mam0045);
       8:
       9:            while ((line = reader.read()) != null) {
      10:
      11:                // Checking the XML element  name;  
      12:                field = line.getFieldValue(XmlConstants.XML_NAME).asString();
      13:                if (field != null && "field".equalsIgnoreCase(field)) {
      14:                    id = line.getFieldValue("id").asString();
      15:                    if (id == null) {
      16:                        id = "";
      17:                    }
      18:                    System.out.println(
      19:                              line.getFieldValue("row").asString() + "\t"
      20:                            + line.getFieldValue("col").asString() + "\t"
      21:                            + (line.getFieldValue("ATTRB").asString()
      22:                               + "                  ").substring(0, 12) + "\t"
      23:                            + (id + "                  ").substring(0, 12) + "\t"
      24:                            + line.getFieldValue("INITIAL").asString()
      25:                    );
      26:                }
      27:            }
      28:
      29:            reader.close();
      30:
      31:        } catch (Exception e) {
      32:
    

     

    JRecord at SourceForge Download Page Forums