### Example: Using record-level access classes to read records from a file

Note: Read the Code example disclaimer for important legal information.

    ///////////////////////////////////////////////////////////////////////////////
    //
    // Record-Level Access example.  This program uses the record-level
    // access classes to read records from a file on the system.
    //
    // Command syntax:
    //    java RLReadFile system
    //
    // This program reads the records from CA/400's sample database file
    // (QCUSTCDT in library QIWS).  If you change this example to update
    // records you should make a copy of QCUSTCDT and update the copy.
    //
    // This source is an example of IBM Toolbox for Java "Record-level access".
    //
    ///////////////////////////////////////////////////////////////////////////////

    import java.io.*;
    import java.util.*;
    import java.math.*;
    import com.ibm.as400.access.*;

    public class RLReadFile extends Object
    {
       public static void main(String[] parameters)
       {

          String system = "";

          // Continue only if a system name was specified.

          if (parameters.length >= 1)
          {

             try
             {

                // Assume the first parameter is the system name.

                system = parameters[0];

                // Create an AS400 object for the system that has the file.

                AS400 as400 = new AS400(system);

                // Create a record description for the file.  The file is QCUSTCDT
                // in library QIWS.

                ZonedDecimalFieldDescription customerNumber =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,0),
                                                               "CUSNUM");
                CharacterFieldDescription lastName =
                              new CharacterFieldDescription(new AS400Text(8, as400), "LSTNAM");

                CharacterFieldDescription initials =
                              new CharacterFieldDescription(new AS400Text(3, as400), "INIT");

                CharacterFieldDescription street =
                              new CharacterFieldDescription(new AS400Text(13, as400), "STREET");

                CharacterFieldDescription city =
                              new CharacterFieldDescription(new AS400Text(6, as400), "CITY");

                CharacterFieldDescription state =
                              new CharacterFieldDescription(new AS400Text(2, as400), "STATE");

                ZonedDecimalFieldDescription zipCode =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(5,0),
                                                               "ZIPCOD");
                ZonedDecimalFieldDescription creditLimit =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(4,0),
                                                               "CDTLMT");
                ZonedDecimalFieldDescription chargeCode =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(1,0),
                                                               "CHGCOD");
                ZonedDecimalFieldDescription balanceDue =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,2),
                                                               "BALDUE");
                ZonedDecimalFieldDescription creditDue =
                              new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,2),
                                                               "CDTDUE");

                // The record format name should be specified for a DDM file.
                // In the case of the QCUSTCDT file, its record format is called CUSREC.

                RecordFormat qcustcdt = new RecordFormat("CUSREC");

                qcustcdt.addFieldDescription(customerNumber);
                qcustcdt.addFieldDescription(lastName);
                qcustcdt.addFieldDescription(initials);
                qcustcdt.addFieldDescription(street);
                qcustcdt.addFieldDescription(city);
                qcustcdt.addFieldDescription(state);
                qcustcdt.addFieldDescription(zipCode);
                qcustcdt.addFieldDescription(creditLimit);
                qcustcdt.addFieldDescription(chargeCode);
                qcustcdt.addFieldDescription(balanceDue);
                qcustcdt.addFieldDescription(creditDue);

                // Create the sequential file object that represents the
                // file on the system.  We use a QSYSObjectPathName object
                // to get the name of the file into the correct format.

                QSYSObjectPathName fileName = new QSYSObjectPathName("QIWS",
                                                                     "QCUSTCDT",
                                                                     "FILE");

                SequentialFile file = new SequentialFile(as400, fileName.getPath());

                // Let the file object know the format of the records.

                file.setRecordFormat(qcustcdt);

                // Open the file for read-only access.  Specify a blocking
                // factor of 10 (the file object will get 10 records when
                // it accesses the system for data).  Do not use commitment
                // control.

                file.open(SequentialFile.READ_ONLY,
                          10,
                          SequentialFile.COMMIT_LOCK_LEVEL_NONE);

                // Read the first record of the file.

                Record data = file.readNext();

                // Loop while there are records in the file (while we have not
                // reached end-of-file).

                while (data != null)
                {

                   // Display the record only if balance due is greater than
                   // zero.  In that case display the customer name and
                   // the balance due.  The following code pulls fields out
                   // of the record by field name.  As the field is retrieved
                   // from the record it is converted from i5/OS format to
                   // Java format.

                   if (((BigDecimal)data.getField("BALDUE")).floatValue() > 0.0)
                   {
                      System.out.print((String) data.getField("INIT") + "  ");
                      System.out.print((String) data.getField("LSTNAM") + "  ");
                      System.out.println((BigDecimal) data.getField("BALDUE"));
                   }

                   // Read the next record in the file.

                   data = file.readNext();
                }

                // When there are no more records to read, disconnect from the system.

                as400.disconnectAllServices();
             }

             catch (Exception e)
             {

                // If any of the above operations failed, print an error message
                // and output the exception.

                System.out.println("Could not read the file");
                System.out.println(e);
             }
          }

          // Display help text when parameters are incorrect.

          else
          {
             System.out.println("");
             System.out.println("");
             System.out.println("");
             System.out.println("Parameters are not correct.  Command syntax is:");
             System.out.println("");
             System.out.println("   RLReadFile as400");
             System.out.println("");
             System.out.println("Where");
             System.out.println("");
             System.out.println("   as400 = system that contains the file");
             System.out.println("");
             System.out.println("For example:");
             System.out.println("");
             System.out.println("   RLReadFile mySystem");
             System.out.println("");
             System.out.println("");
             System.out.println("Note, this program reads data base file QIWS/QCUSTCDT.  ");
             System.out.println("");
             System.out.println("");
          }

          System.exit(0);

       }
    }