为什么你需要获得 Field Level ???。要将文件转换为 ascii,您不需要字段级别。
实用程序转换程序
要将 Cobol 文件转换为 ascii,您可以使用以下实用程序之一:
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 处理