Menu

#8 CobolBuilder writing records to single line

open
None
2
2018-01-12
2018-01-10
Anonymous
No

Hi,

I am using jrecord to write records to file. But i writer is writing all records to a single. Can you please
check if below code is correct.

    final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
        "copybook").setFont("CP1252"); 
     AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
     AbstractLine outline = null;
     while(resultSet.next()){
       outline = ioBuilder.newLine();
       outline.getField("id").set(resultSet.getInt("id")));

       writer.write(outline);
       }
       writer.close();


  Excpected  Output:
  1234
  5677

  Receiving o/p:
  12345677

  Can you please update how i write individual record into multiple lines

  Thanks
  babu

Discussion

  • Bruce Martin

    Bruce Martin - 2018-01-10
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -2,7 +2,8 @@
    
      I am using jrecord to write records to file. But i writer is writing all records to a single. Can you please
      check if below code is correct.
    
    - 
    + ~~~
    +
      final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
        "copybook").setFont("CP1252");
          AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
    @@ -14,6 +15,7 @@
            writer.write(outline);
            }
            writer.close();
    +~~~
    
           Excpected  Output:
           1234
    
     
  • Bruce Martin

    Bruce Martin - 2018-01-10
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -2,10 +2,10 @@
    
      I am using jrecord to write records to file. But i writer is writing all records to a single. Can you please
      check if below code is correct.
    
    + 
      ~~~
    -
    - final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
    -   "copybook").setFont("CP1252");
    +  final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
    +       "copybook").setFont("CP1252"); 
          AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
          AbstractLine outline = null;
          while(resultSet.next()){
    
     
  • Bruce Martin

    Bruce Martin - 2018-01-10
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -3,19 +3,19 @@
      I am using jrecord to write records to file. But i writer is writing all records to a single. Can you please
      check if below code is correct.
    
    
    - ~~~
    -  final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
    -       "copybook").setFont("CP1252"); 
    -     AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
    -     AbstractLine outline = null;
    -     while(resultSet.next()){
    -       outline = ioBuilder.newLine();
    -       outline.getField("id").set(resultSet.getInt("id")));
    +
    +    final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
    +            "copybook").setFont("CP1252"); 
    +         AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
    +         AbstractLine outline = null;
    +         while(resultSet.next()){
    +           outline = ioBuilder.newLine();
    +           outline.getField("id").set(resultSet.getInt("id")));
    
    
    -       writer.write(outline);
    -       }
    -       writer.close();
    -~~~
    +           writer.write(outline);
    +           }
    +           writer.close();
    +
    
           Excpected  Output:
           1234
    
     
  • Bruce Martin

    Bruce Martin - 2018-01-10
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -4,7 +4,7 @@
      check if below code is correct.
    
    
    
    -    final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
    +        final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL.newIOBuilder(
                 "copybook").setFont("CP1252"); 
              AbstractLineWriter writer =  ioBuilder.newWriter("output.txt");
              AbstractLine outline = null;
    
     
  • Bruce Martin

    Bruce Martin - 2018-01-10

    What you need to do is set the setFileOrganization(Constants.IO_BIN_TEXT):

    final ICobolIOBuilder ioBuilder = JRecordInterface1.COBOL
                .newIOBuilder("copybook")
                        .setFileOrganization(Constants.IO_BIN_TEXT)
                        .setFont("CP1252");
    
     
  • Anonymous

    Anonymous - 2018-01-10

    Thank you!Bruce .It is working perfectly now. Can you update how Constants.Io_BIN_TEXT helped here?

     
  • Bruce Martin

    Bruce Martin - 2018-01-11

    JRecord is designed to work with Cobol data structures. The setFileOrganization
    tells JRecords are to be organised - i.e. what the line seperator is.

    Fixed width Records

    With length records, each record is a fixed length. There is no /r / n etc between individual records.

    <--Record 1 --><--Record 2 --><--Record 3 -->
    

    This is what was being used initially. JRecord provides 2 versions of fixed length records

              .setFileOrganization(Constants.IO_FIXED_LENGTH_RECORDS)
    

    does byte (stream based IO). You can use it for single byte charactersets / binary files.

              .setFileOrganization(Constants.IO_FIXED_LENGTH_CHAR)
    

    Used for multi-byte character-sets (e.g.UTF-8, UTF-16)

    VB Records

    With Variable length records you have

       <record-length><--- record data  --->
       <record-length><---   record data     --->
       <record-length><--- record data --->
       <record-length><---        record data       --->
    

    JRecord provides several VB formats Constants.IO_VB, Constants.IO_VB_DUMP, Constants.IO_VB_FUJITSU, Constants.IO_VB_GNU_COBOL

    Text files

    For standard text files you can use

             .setFileOrganization(Constants.IO_BIN_TEXT)
    

    or

             .setFileOrganization(Constants.IO_UNICODE_TEXT)
    

    With Constants.IO_BIN_TEXT all IO is done at byte (stream level).

    With Constants.IO_UNICODE_TEXT standard Java Read / writers are used

     

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB