【问题标题】:How do I identify the level of a field in copybook using JRecord in Java?如何在 Java 中使用 JRecord 识别字帖中字段的级别?
【发布时间】:2017-08-06 06:15:29
【问题描述】:

我正在尝试读取 EBCDIC 文件并将其转换为 Java 中的 ASCII 格式,这需要借助字帖。我正在使用 JRecord 来阅读字帖。那么现在,如何使用 JRecord 从字帖中获取字段级别?

编辑 1:

请原谅我提出一个模糊的问题。我没有大型机或cobol的经验。如果有帮助,我将添加更多细节。

我的源文件包含多个交易详情。字帖包含有关交易的信息以及与该特定交易相关的字段。

我必须将每个交易及其字段拆分到一个单独的文件中(包含一个交易和相应的字段)。

CopyBook

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

感谢您的帮助。

【问题讨论】:

  • Priya 欢迎来到 stackoverflow。通常预计人们会make an effortshow 他们已经尝试过。这使回答问题的人免于尝试相同的事情。此外,问题需要更多precisedirect 关于您正在尝试做的事情。如果要转换文件 ascii,我不明白为什么需要 Cobol 级别数字。我提供了一个涵盖多种可能性的通用答案。
  • 非常感谢马丁。我已经根据实际要求更新了我的问题。给我你的建议。提前致谢。

标签: java cobol ebcdic copybook jrecord


【解决方案1】:

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


实用程序转换程序

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

  • Cobol2Csv 子项目 - 将 Cobol 数据文件转换为 Csv 文件
  • Cobol2Xml 子项目 - 将 Cobol 数据文件转换为 Xml 文件
  • Cobol2Jsonjson实用程序

Cobol 文件的 Java 处理

如果你想对文件做一些处理,你可以使用Generate RecordEditor 的函数从 Cobol 字帖生成示例 JRecord 代码。

使用标准模板生成的代码

如果您使用 standard 模板,RecordEditor 将生成如下代码:

    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();
    }

使用 lineWrapper 模板生成的代码

  AbstractLine line;
    int lineNum = 0;

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


       LineDtar020JR lineDtar020JR = new LineDtar020JR();


       AbstractLineReader reader = iob.newReader(dataFile);
       while ((line = reader.read()) != null) {
           lineNum += 1;
           lineDtar020JR.setLine(line);
           System.out.println(
                          lineDtar020JR.getKeycodeNo() 
                  + " " + lineDtar020JR.getStoreNo() 
                  + " " + lineDtar020JR.getDate() 
                  + " " + lineDtar020JR.getDeptNo() 
                  + " " + lineDtar020JR.getQtySold() 
                  + " " + lineDtar020JR.getSalePrice() 
               );
        }

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

        e.printStackTrace();
    }

通用 Cobol 处理

如果您想进行更通用的处理,您可以使用 fieldIterator

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

JRecord 示例

在最新版本的JRecord0.81.4中有示例在Source/JRecord_IO_Builder_Examples/src目录


树处理

如果您需要使用 JRecord 访问级别编号,请使用 CobolSchemaReader.newCobolSchemaReader(...) 接口。

您还可以查看Cobol2Xml 子项目的代码。它通过扩展 CobolSchemaReader

进行tree 处理

【讨论】:

    【解决方案2】:

    阅读 http://www.catb.org/~esr/faqs/smart-questions.htmlhttps://www.mikeash.com/getting_answers.html 关于提问

    但无论如何:

    使用代码生成

    • 输入 Cobol Copybook,样本 cobol 文件(如果有的话)。你会 可能能够使用Split Copybook On Redefines选项

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

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

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

          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()
                 );
              }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2011-11-27
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      相关资源
      最近更新 更多