如何在Java中使用JRecord识别copybook中字段的级别?

如何在Java中使用JRecord识别copybook中字段的级别?,java,cobol,ebcdic,copybook,jrecord,Java,Cobol,Ebcdic,Copybook,Jrecord,我正在试图读取一个EBCDIC文件,并在copybook的帮助下将其转换为Java中的ASCII格式。我正在使用JRecord阅读文案。那么现在,如何使用JRecord从copybook获取字段级别呢 编辑1: 请原谅我提了个模糊的问题。我没有大型机或cobol方面的经验。如果可能的话,我会补充一些细节 我的源文件包含多个事务详细信息。副本包含有关交易的信息以及与该特定交易相关的字段 我必须将每个事务及其字段拆分为一个单独的文件(包含一个事务和相应的字段) 在附加的copybook中,第1行中

我正在试图读取一个EBCDIC文件,并在copybook的帮助下将其转换为Java中的ASCII格式。我正在使用JRecord阅读文案。那么现在,如何使用JRecord从copybook获取字段级别呢

编辑1:

请原谅我提了个模糊的问题。我没有大型机或cobol方面的经验。如果可能的话,我会补充一些细节

我的源文件包含多个事务详细信息。副本包含有关交易的信息以及与该特定交易相关的字段

我必须将每个事务及其字段拆分为一个单独的文件(包含一个事务和相应的字段)

在附加的copybook中,第1行中的字段可以具有第2行到第4行之间的值。如果EXTRA-TYPE是01,那么我必须读取第6行到第11行中的字段。类似地,如果EXTRA-TYPE是02,那么我必须读取第12行到第16行中的字段。 我正在尝试动态拆分事务类型及其各自的字段。 (我需要获得字段相对于第1行中事务类型的开始和结束位置)如何在Java中实现这一点


感谢您的帮助。

您为什么需要获得现场级别???。要将文件转换为ascii,不需要字段级别


效用转换程序 要将Cobol文件转换为ascii,可以使用以下实用程序之一:

  • 子项目-将Cobol数据文件转换为Csv文件
  • 子项目-将Cobol数据文件转换为Xml文件
  • json实用性
Cobol文件的Java处理 如果要对文件进行一些处理,可以使用生成 从Cobol copybook生成示例代码的函数

使用标准模板生成的代码 如果使用标准模板,将生成如下代码:

    AbstractLine line;
    int lineNum = 0;

    try {
        ICobolIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder(copybookName)
                                       .setFont("cp037")
                                       .setFileOrganization(Constants.IO_FIXED_LENGTH)
                                       .setSplitCopybook(CopybookLoader.SPLIT_NONE)
                                   ;  


        FieldNamesDtar020.RecordDtar020 rDtar020 = FieldNamesDtar020.RECORD_DTAR020;
        AbstractLineReader reader = iob.newReader(dataFile);
        while ((line = reader.read()) != null) {
            lineNum += 1;
            System.out.println(
                          line.getFieldValue(rDtar020.keycodeNo).asString()
                  + " " + line.getFieldValue(rDtar020.storeNo).asString()
                  + " " + line.getFieldValue(rDtar020.date).asString()
                  + " " + line.getFieldValue(rDtar020.deptNo).asString()
                  + " " + line.getFieldValue(rDtar020.qtySold).asString()
                  + " " + line.getFieldValue(rDtar020.salePrice).asString()
               );
        }

        reader.close();
    } catch (Exception e) {
        System.out.println("~~> " + lineNum + " " + e);
        System.out.println();

        e.printStackTrace();
    }
    ICobolIOBuilder iob = JRecordInterface1.COBOL
                               .newIOBuilder(copybookName)
                                   .setFileOrganization(Constants.IO_BIN_TEXT)
                                   .setSplitCopybook(CopybookLoader.SPLIT_REDEFINE)
                               ;  


    FieldNamesAmspodownloadRedef1.RecordPoHeaderRecord rPoHeaderRecord = FieldNamesAmspodownloadRedef1.RECORD_PO_HEADER_RECORD;
    FieldNamesAmspodownloadRedef1.RecordProductRecord rProductRecord = FieldNamesAmspodownloadRedef1.RECORD_PRODUCT_RECORD;
    FieldNamesAmspodownloadRedef1.RecordLocationRecord rLocationRecord = FieldNamesAmspodownloadRedef1.RECORD_LOCATION_RECORD;
    AbstractLineReader reader = iob.newReader(dataFile);
    while ((line = reader.read()) != null) {
        lineNum += 1;
        if (
           "H1".equals(line.getFieldValue(rPoHeaderRecord.recordType).asString())
        )  {

           System.out.println(
                      line.getFieldValue(rPoHeaderRecord.recordType).asString()
              + " " + line.getFieldValue(rPoHeaderRecord.sequenceNumber).asString()
              + " " + line.getFieldValue(rPoHeaderRecord.vendor).asString()
              + " " + line.getFieldValue(rPoHeaderRecord.po).asString()
                ....
              + " " + line.getFieldValue(rPoHeaderRecord.cancelByDate).asString()
              + " " + line.getFieldValue(rPoHeaderRecord.ediType).asString()
           );
        }
        if (
           "D1".equals(line.getFieldValue(rProductRecord.recordType).asString())
        )  {

           System.out.println(
                      line.getFieldValue(rProductRecord.recordType).asString()
              + " " + line.getFieldValue(rProductRecord.packQty).asString()
              + " " + line.getFieldValue(rProductRecord.packCost).asString()
              + " " + line.getFieldValue(rProductRecord.apn).asString()
              + " " + 
                  .....
              + " " + line.getFieldValue(rProductRecord.productName).asString()
           );
        }
        if (
           "S1".equals(line.getFieldValue(rLocationRecord.recordType).asString())
        )  {

           System.out.println(
                      line.getFieldValue(rLocationRecord.recordType).asString()
              + " " + line.getFieldValue(rLocationRecord.dcNumbe.get(0)).asString()
              + " " + line.getFieldValue(rLocationRecord.packQuantit.get(0)).asString()
           );
        }
使用lineWrapper模板生成的代码
通用Cobol处理 如果您想进行更一般的处理,可以使用字段迭代器

FieldIterator fieldIterator = line.getFieldIterator("Record-Name");

JRecord示例 在最新版本的0.81.4中,在Source/JRecord\u IO\u Builder\u examples/src目录中有一些示例


树木处理 如果需要使用JRecord访问级别号,请使用cobolschemaAreader.newcobolschemaAreader(…)接口

您还可以查看子项目的代码。它通过扩展cobolschemaAreader进行
处理

读取 或者问问题

但无论如何:

使用代码生成
  • 从USB版本下载Recordeditor无需安装-只需解压缩即可

  • 启动RecordEditor并选择generate选项

  • 输入Cobol副本,示例Cobol文件(如果有)。你会 可能能够在重定义选项上使用拆分副本

  • 记录面板上,在记录类型字段中输入DA147-EXTRA-TYPE

  • 按下生成代码按钮。在下一个屏幕上,您可以选择模板。标准模板是一个很好的起点

  • 按下生成代码按钮,程序应生成一些示例代码,如:

        AbstractLine line;
        int lineNum = 0;
    
        try {
            ICobolIOBuilder iob = JRecordInterface1.COBOL
                                       .newIOBuilder(copybookName)
                                           .setFont("cp037")
                                           .setFileOrganization(Constants.IO_FIXED_LENGTH)
                                           .setSplitCopybook(CopybookLoader.SPLIT_NONE)
                                       ;  
    
    
            FieldNamesDtar020.RecordDtar020 rDtar020 = FieldNamesDtar020.RECORD_DTAR020;
            AbstractLineReader reader = iob.newReader(dataFile);
            while ((line = reader.read()) != null) {
                lineNum += 1;
                System.out.println(
                              line.getFieldValue(rDtar020.keycodeNo).asString()
                      + " " + line.getFieldValue(rDtar020.storeNo).asString()
                      + " " + line.getFieldValue(rDtar020.date).asString()
                      + " " + line.getFieldValue(rDtar020.deptNo).asString()
                      + " " + line.getFieldValue(rDtar020.qtySold).asString()
                      + " " + line.getFieldValue(rDtar020.salePrice).asString()
                   );
            }
    
            reader.close();
        } catch (Exception e) {
            System.out.println("~~> " + lineNum + " " + e);
            System.out.println();
    
            e.printStackTrace();
        }
    
        ICobolIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder(copybookName)
                                       .setFileOrganization(Constants.IO_BIN_TEXT)
                                       .setSplitCopybook(CopybookLoader.SPLIT_REDEFINE)
                                   ;  
    
    
        FieldNamesAmspodownloadRedef1.RecordPoHeaderRecord rPoHeaderRecord = FieldNamesAmspodownloadRedef1.RECORD_PO_HEADER_RECORD;
        FieldNamesAmspodownloadRedef1.RecordProductRecord rProductRecord = FieldNamesAmspodownloadRedef1.RECORD_PRODUCT_RECORD;
        FieldNamesAmspodownloadRedef1.RecordLocationRecord rLocationRecord = FieldNamesAmspodownloadRedef1.RECORD_LOCATION_RECORD;
        AbstractLineReader reader = iob.newReader(dataFile);
        while ((line = reader.read()) != null) {
            lineNum += 1;
            if (
               "H1".equals(line.getFieldValue(rPoHeaderRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rPoHeaderRecord.recordType).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.sequenceNumber).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.vendor).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.po).asString()
                    ....
                  + " " + line.getFieldValue(rPoHeaderRecord.cancelByDate).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.ediType).asString()
               );
            }
            if (
               "D1".equals(line.getFieldValue(rProductRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rProductRecord.recordType).asString()
                  + " " + line.getFieldValue(rProductRecord.packQty).asString()
                  + " " + line.getFieldValue(rProductRecord.packCost).asString()
                  + " " + line.getFieldValue(rProductRecord.apn).asString()
                  + " " + 
                      .....
                  + " " + line.getFieldValue(rProductRecord.productName).asString()
               );
            }
            if (
               "S1".equals(line.getFieldValue(rLocationRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rLocationRecord.recordType).asString()
                  + " " + line.getFieldValue(rLocationRecord.dcNumbe.get(0)).asString()
                  + " " + line.getFieldValue(rLocationRecord.packQuantit.get(0)).asString()
               );
            }
    

Priya欢迎来到stackoverflow。人们通常希望人们做出努力,展示他们的尝试。这就避免了回答问题的人尝试同样的事情。另外,问题需要更加精确,并且更加直接地说明你想做什么。我不明白,如果要转换ascii文件,为什么需要Cobol级别的数字。我已经提供了一个涵盖多种可能性的通用答案。非常感谢Martin。我已经根据实际需要更新了我的问题。给我你的建议。提前谢谢。