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:
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.