How to convert Java object to binary file using copybook

1.5k views Asked by At

Is anyone aware of how to convert a Java object into a binary file using a COBOL copybook? The file is to be transmitted to mainframe and hence has to have the mapping with copybook. I tried looking at JRecord but I could find just the conversion from mainframe binary file into java object and not the other way round. Can someone please help with it.

1

There are 1 answers

1
Bruce Martin On

Have a look at question How do you generate java~jrecord code fror a Cobol copybook . It demonstrated how the use the Recordeditor to generate Java / JRecord code to read / write a file.

but broadly

    IIOBuilder ioBldr = JRecordInterface1.SCHEMA_XML .newIOBuilder(copybookName);


        try {
            AbstractLine saleRecord = ioBldr.newLine();
            AbstractLineWriter writer  = ioBldr.newWriter(salesFileOut);

            saleRecord.getFieldValue("KEYCODE-NO").set(1331);
            saleRecord.getFieldValue("STORE-NO").set(1);
            saleRecord.getFieldValue("DATE").set(80921);
            saleRecord.getFieldValue("DEPT-NO").set(100);
            saleRecord.getFieldValue("QTY-SOLD").set(7);
            saleRecord.getFieldValue("SALE-PRICE").set(7.00);
            writer.write(saleRecord);

            saleRecord.getFieldValue("STORE-NO").set(11);
            writer.write(saleRecord);

            saleRecord.getFieldValue("STORE-NO").set(121);
            writer.write(saleRecord);

            System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
            writer.close();
        } catch (Exception e) {

While this uses an Xml-Schema; the principle is the same.