Menu

How to write multiple lines with different layouts into a file?

2024-05-14
2024-05-22
  • Martin Scheibe

    Martin Scheibe - 2024-05-14

    Hello,

    (First and foremost, i like to thank Bruce Martin for his dedication, work and support. I think he helped a lot of people.)

    Im trying to build a custom .bin file using the JRecord libary.
    The file im trying to write has 3 kinds of lines (AbstractLines) each with a different layoutDetail.

    I can get one layoutDetail from a copybook (in .CPY format) but the other two layouts i have to custom build.

    So far i got code for the copybook layoutDetail, what seems to work as intended.

    LayoutDetail layoutDetail =
    JRecordInterface1.COBOL
                    .newIOBuilder(copybook)
                    .setFont("IBM01141")
                    .setFileOrganization(IO_FIXED_LENGTH)
                    .getLayout();
    

    This is my attempt to build a custom layoutDetail:

    LayoutDetail layoutDetail = JRecordInterface1.FIXED_WIDTH
                    .newIOBuilder()
                    .setFont("IBM01141")
                    .defineFieldsByLength()
                    .addFieldByLength("TEST1", Type.ftChar, 1, 0)
                    .addFieldByLength("TEST2", Type.ftChar, 1000, 0)
                    .endOfRecord()
                    .getLayout();
    

    This is the writer im using:

    AbstractLineWriter writer = JRecordInterface1
                    .COBOL
                    .newIOBuilder(copybook)
                    .setFont("IBM01141")
                    .setFileOrganization(IO_FIXED_LENGTH)
                    .newWriter(fileOut);
    

    This is how im creating a new AbstractLine and feed it with the respective layoutDetail:

    AbstractLine line =
    LineIOProvider.getInstance().getLineProvider(layoutDetail).getLine(layoutDetail);
    

    The problem i run into is, that i cant seem to write these lines into the same file, without one layout, overwriting the other.
    For example the first line has a length of for example 1000, and this length gets applied to the other lines im trying to write, even when theyre shorter or longer.

    -Am i on the right approach or am i doing it the wrong way?
    -How do I achieve, that a line is dynamically sometimes shorter, sometimes longer, depending if a field is filled or not, is this possible?

    Also on another note:
    -How do i read a copybook in XML format, like this:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <copybook dialect="Mainframe" filename="XXXX.CPY">
        <item display-length="189" level="30" name="X" position="1" storage-length="177">
            <item display-length="2" level="35" name="XX" numeric="true" picture="9(002)" position="1" storage-length="2"/>
            <item display-length="15" level="35" name="XXX" numeric="true" picture="9(015)" position="3" storage-length="15"/>
            <item display-length="10" level="35" name="XXXX" picture="X(010)" position="18" 
        etc...
        etc...
        etc...   
        </item>
    </copybook>
    

    Thanks in advance for any answers.

     

    Last edit: Martin Scheibe 2025-05-13
  • Bruce Martin

    Bruce Martin - 2024-05-15

    You are using IBM01141 - EBCDIC encoding

    so I presume you are either

    • Going to send the file to mainframe (most likely)
    • Final Application to run on the mainframe (very unlikely)

    Assuming you are sending to the mainframe, The two easiest options are

    • Write in EBCDIC FB as you are doing -easy safest transfer, will handle binary files
    • Write in ASCII/UTF-8 and use IFileStructureConstants.IO_STANDARD_TEXT_FILE
      and do the UTF-8 ===> EBCDIC translation when transfering the file.
      This only works for text files, binary files get corrupted. There can be Translation issues.

    For small files I would just use the larger record-length and IO_FIXED_LENGTH
    as you are doing an accept the wasted space.


    Fixed Length IO_FIXED_LENGTH

    The File-Structure of IO_FIXED_LENGTH means every record is the same length
    with no End-of-line makings (i.e. no \n or \r characters). i.e.

       <====  Record-1  =================================>
       <====  Record-2  =================================>
                         ...
       <====  Record-1  =================================>
    

    This is normal in Cobol with a single copybook used in the file.

    Windows/Linux Style IO_STANDARD_TEXT_FILE

    If you use IFileStructureConstants.IO_STANDARD_TEXT_FILE with IBM01141, you get
    a standard windows/linux style file but it probably will not be understood
    on the mainframe.

    VB Files (IO_VB)

    You could use IFileStructureConstants.IO_VB, you might have problems
    transfering to the mainframe (my experience is from a long time ago, so things
    might of changed). For FTP use the RDW option

    Format of VB File

        <RDW><====  Record-1  =================================>
        <RDW><====  Record-2  ========>
        <RDW><====  Record-3  ==================>
          ...
    

    Where RDW is the record-descriptor word (4 bytes, it includes a 2 byte legth).


    ## Xml copybook

    It looks like the xml produced by cb2xml. To process it

         LayoutDetail bldr =
               JRecordInterface1.CB2XML
                    .newIOBuilder(copybook)
                    .setFont("IBM01141")
                    .setFileOrganization(IO_FIXED_LENGTH);
         LayoutDetail layoutDetail = bldr .getLayout();
    

    It basically works like JRecordInterface1.COBOL

     

    Last edit: Bruce Martin 2024-05-15
    • Martin Scheibe

      Martin Scheibe - 2024-05-21

      Hello Bruce,
      Thanks a lot for your reply.

      I did as you mentioned and just accepted the wasted space. I just filled the shorter lines with spaces, so they are the same length as the longest line.

      JRecordInterface1.CB2XML worked with the xml copybooks aswell. Thanks again.

      You are correct, i want to send a the files i create to a mainframe, kind of. I need the files to be the exact same as the ones the mainframe can work with. But I didn't actually do this yet.

       
  • Bruce Martin

    Bruce Martin - 2024-05-22

    There should not be any problem working with a FB file on the mainframe. If the program is expecting a VB file, you can use Sort or IEBCOPY etc utilites to do the conversion from FB to VB

    This should work.


    If the file is Only text, you could write as a normal ascii text file and let FTP do the conversion (and write a VB file


    I did do a some google searches and could not find any way to send a VB file to the mainframe; you could ask some experienced mainframe people


    Finally running Java/JRecord on the mainframe would allow the creation of VB files using java-zos services

     

    Last edit: Bruce Martin 2024-05-22

Log in to post a comment.