CSV files are a simple format to store column-based records. They can be edited in Excel or directly as text.
In this short example we are going to process CSV files with RPG programs. The analogy is simple, one line in a CSV file corresponds to a record on the AS400. Here is our input data stored as CSV files:
DATA00.CSV ---------- clientCode;clientName;masterCode "123";"Dupont";"x" "124";"Jean";"x" "234";"Smith";"y" "432";"Dupuis";"z"
DATA01.CSV ---------- clientCode;category;itemName;itemCode "001";"1";"Aaron";"56789" "002";"1";"Booz";"4568" "002";"2";"Chris";"789" "123";"3";"Denis";"0987678" "123";"2";"Ellen";"234562" "125";"1";"Franz";"787876" "125";"2";"Gégé";"34573" "234";"1";"Hubert";"956874" "234";"3";"Isa";"67807" "235";"2";"Joëlle";"78907890" "432";"1";"Klaus";"67098"
Here is our sample RPG program. It reads the two input files, combines their data, and writes the result.
C*********************************************************************** FDATA00 IF E K DISK FDATA01 IF E K DISK FDATA02 O E K DISK C*********************************************************************** C**/ C KEY1 KLIST C KFLD ABC800 C*********************************************************************** C READ DATA00 90 C *IN90 DOWEQ '0' C* C KEY1 SETLL DATA01 C KEY1 READE DATA01 91 C *IN91 DOWEQ '0' C EXSR #WRITE C KEY1 READE DATA01 91 C END C* C READ DATA00 90 C END C* C SETON LR C RETURN C*********************************************************************** C* #WRITE ROUTINE C*********************************************************************** C #WRITE BEGSR C MOVE ABC800 DEF001 C MOVE ABC260 DEF002 C MOVE IJK134 DEF003 C MOVE IJK740 DEF004 C WRITE DATA02 C ENDSR C***********************************************************************
Now we’re going tp convert this program to Java using Neogram’s RPG converter. The conversion strategy that we select consists in emulating all file operations with a runtime Java library that provides support for CSV files (this runtime library is provided with the converter).
Emulating file operations is not the most Java-like option (look at our SQL example), but it’s simple, close to the original code and relatively easy to learn for Java developers.
Here is the output of the conversion (truncated for readability). Note that we used the converter interactive GUI to provide meaningful Java names:
public void main() { in90 = data00.read(); while(in90 == false) { // data01.setll(key1); in91 = data01.reade(key1); while(in91 == false) { _write(); in91 = data01.reade(key1); } // in90 = data00.read(); } data02.close(); data00.close(); data01.close(); } private void _write() { data02.ioRecord.clientCode = data00.ioRecord.clientCode; data02.ioRecord.clientName = data00.ioRecord.clientName; data02.ioRecord.itemCode = data01.ioRecord.itemCode; data02.ioRecord.itemName = data01.ioRecord.clientCode; data02.write(); }
Now we compile and run this Java program. Here is its output, written in a CSV file:
DATA02.CSV ---------- "CLIENTCODE";"CLIENTNAME";"ITEMCODE";"ITEMNAME" "123";"Dupont";"234562";"123" "123";"Dupont";"987678";"123" "234";"Smith";"67807";"234" "234";"Smith";"956874";"234" "432";"Dupuis";"67098";"432"