1

I have a use case wherein I need to create a file for the mainframe, which contains text and packed decimal using Java.

I have gone through a lot of threads on Stack Overflow, but nothing seems to be working. I am using JTOpen library.

Here is a sample program I wrote:

package com.amazonaws.samples.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.StringJoiner;

import com.ibm.as400.access.AS400PackedDecimal;

public class PackedDecimalTest {

    public static void main(String args[]) throws IOException {
        AS400PackedDecimal packedDecimal = new AS400PackedDecimal(8, 0);
        BigDecimal javaBigDecimal = new BigDecimal("20200521");
        byte[] convertedBytesArray = packedDecimal.toBytes(javaBigDecimal);

        try (FileOutputStream fos = new FileOutputStream("test.txt", false)) {

            //StringJoiner joiner = new StringJoiner(" ");
            //String firstName = new String("firstName1");
            //String lastName = new String("lastName1");
            //String empId = new String("1111");
            //String dept = new String("empDept1");
            //String n = new String("N");
            //joiner = joiner.add(firstName).add(lastName).add(empId).add(dept).add(n);
            //fos.write(joiner.toString().getBytes());
            fos.write(convertedBytesArray);

        }

        try (FileInputStream fis = new FileInputStream("test.txt")) {
            BigDecimal convertedBigDecimal = (BigDecimal) packedDecimal.toObject(fis.readAllBytes());
            System.out.println(convertedBigDecimal.toString());
        }
    }

}

When I write the file and read it immediately, it seems to be working fine in Java. I was able to see the output properly.

However, when the same file is opened in the mainframe, I don't see packed decimal data properly.

Here is how I see data in the mainframe:

mainframe file

I'm pretty much stuck on what needs to be done to fix it. Any pointers on what I am doing wrong? How can I write both ASCII and packed decimal data in the same file? Do I need to define any character set before writing a file for the mainframe to properly read data? Can I write it in a .txt file, or the file extension should be different or the extension have no significance? I have no knowledge on mainframe and file formats and character sets it supports.

4

2 回答 2

1

鉴于压缩十进制实际上是一个“二进制”值,您需要使用大型机的编码(CP-037 或 CP-1047 或您的首选代码页)和压缩十进制“二进制”字段创建文件,然后将文件作为二进制传输。我不熟悉用于 AS/400 的代码页,但无论如何都是同样的问题。

如果您使用scp该文件,则会进行隐式翻译,这将损坏该文件。

使用 FTP / FTPS 使用二进制模式进行传输。

这是从一个代码页转换为另一个代码页的方法的链接。

将字符串从一个字符集转换为另一个字符集

于 2021-01-14T15:00:23.337 回答
0

您看到的损坏来自将 ASCII 文件转换为 EBCDIC。您需要将文件写入 EBCDIC 并执行二进制(直接无翻译)传输到大型机。

根据@Hogstroms 的回答,

改变

        fos.write(joiner.toString().getBytes());

        fos.write(joiner.toString().getBytes("cp037"));

这会将文件写入为 EBCDIC。您需要将大型机传输更改为二进制。


您可能会发现JRecord很有用,特别是如果您有 cobol Copybook。

于 2021-01-14T21:00:50.407 回答