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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
praveen
  • 11
  • 1

2 Answers2

1

Given that packed decimal is effectively a "binary" value you'll need to create the file with the encoding of the mainframe (CP-037 or CP-1047 or what is your preferred codepage) and the packed decimal "binary" fields and then transfer the file as binary. I'm not familiar with the code pages used for AS/400 but its the same issue regardless.

If you use scp the file will undergo an implicit translation which will corrupt the file.

Use FTP / FTPS using binary mode for the transfer.

Here is a link on an approach to convert from one codepage to another.

Converting String from One Charset to Another

Hogstrom
  • 3,581
  • 2
  • 9
  • 25
  • Thanks @Hogstrom, when writing the file in java, can we write it in text file or any specific file format ? – praveen Jan 14 '21 at 16:31
  • I think this link will help you with the feature you are looking to implement https://stackoverflow.com/questions/29667977/converting-string-from-one-charset-to-another – Hogstrom Jan 14 '21 at 16:56
0

The corruption you are seeing is from converting an ASCII file to EBCDIC. You need to write the file as EBCDIC and do a binary (straight no translation) transfer to the mainframe.

Following from @Hogstroms answer,

Change

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

to

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

This will write the file as EBCDIC. You will need to change the Mainframe transfer to binary.


You may find JRecord useful, particularly if you have a cobol Copybook.

Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
  • thanks @Bruce Martin, will try the code snippet you have provided – praveen Jan 15 '21 at 03:32
  • hi @Bruce Martin, i tried to use JRecord, all the data written in file is coming as single line when we imported it in Mainframe. how does Mainframe know where the line ends? is it based on length defined in copy book? i am trying to test the file generated through Jrecord by importing into Mainframe without copy book. – praveen Jan 25 '21 at 02:36
  • @praveen It is in the File definition on the mainframe. You can either specify recfm=fb,lrecl/b;lsize etc when send the file or send to an existing mainfram with the correct attributes – Bruce Martin Jan 25 '21 at 12:05