Example: Using record-level access classes

Note: Read the Code example disclaimer for important legal information.
//////////////////////////////////////////////////////////////////////////////////
//
// Record level access example.  This program will prompt the user
// for the name of the server and the file to display.  The file must exist
// and contain records.  Each record in the file will be displayed
// to System.out.
//
// Calling syntax: java RLSequentialAccessExample
//
// This source is an example of IBM Toolbox for Java "RecordLevelAccess"
//
//////////////////////////////////////////////////////////////////////////////////



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

public class RLSequentialAccessExample
{
   public static void main(String[] parmeters)
   {
      // Created a reader to get input from the user
      BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);

      // Declare variables to hold the system name, library, file and member names
      String systemName = "";
      String library = "";
      String file = "";
      String member = "";

      // Get the system name and and file to display from the user
      System.out.println();
      try
      {
        System.out.print("System name: ");
        systemName = inputStream.readLine();

        System.out.print("Library in which the file exists: ");
        library = inputStream.readLine();

        System.out.print("File name: ");
        file = inputStream.readLine();

        System.out.print("Member name (press enter for first member): ");
        member = inputStream.readLine();
        if (member.equals(""))
        {
          member = "*FIRST";
        }

        System.out.println();
      }
      catch (Exception e)
      {
        System.out.println("Error obtaining user input.");
        e.printStackTrace();
        System.exit(0);
      }

      // Create AS400 object and connect for the record level access service.
      AS400 system = new AS400(systemName);
      try
      {
        system.connectService(AS400.RECORDACCESS);
      }
      catch(Exception e)
      {
        System.out.println("Unable to connect for record level access.");
        System.out.println("Check the readme file for special instructions regarding record
                           level access");
        e.printStackTrace();
        System.exit(0);
      }

      // Create a QSYSObjectPathName object to obtain the integrated file system path name form
      // of the file to be displayed.
      QSYSObjectPathName filePathName = new QSYSObjectPathName(library, file, member, "MBR");

      // Create a SequentialFile object representing the file to be displayed
      SequentialFile theFile = new SequentialFile(system, filePathName.getPath());

      // Retrieve the record format for the file
      AS400FileRecordDescription recordDescription = 
        new AS400FileRecordDescription(system, filePathName.getPath());
      try
      {
        RecordFormat[] format = recordDescription.retrieveRecordFormat();

        // Set the record format for the file
        theFile.setRecordFormat(format[0]);

        // Open the file for reading.  Read 100 records at a time if possible.
        theFile.open(AS400File.READ_ONLY, 100, AS400File.COMMIT_LOCK_LEVEL_NONE);

        // Display each record in the file
        System.out.println("Displaying file " + library.toUpperCase() + "/"
                           + file.toUpperCase() + "(" + theFile.getMemberName().trim() + "):");

        Record record = theFile.readNext();
        while (record != null)
        {
          System.out.println(record);
          record = theFile.readNext();
        }
        System.out.println();

        // Close the file
        theFile.close();

        // Disconnect from the record level access service
        system.disconnectService(AS400.RECORDACCESS);
      }
      catch (Exception e)
      {
        System.out.println("Error occurred attempting to display the file.");
        e.printStackTrace();

        try
        {
          // Close the file
          theFile.close();
        }
        catch(Exception x)
        {
        }

        // Disconnect from the record level access service
        system.disconnectService(AS400.RECORDACCESS);
        System.exit(0);
      }

      // Make sure that the application ends; see readme for details
      System.exit(0);
    }
  }