KeyedFile

The KeyedFile class gives a Java™ program keyed access to a file on the server. Keyed access means that the Java program can access the records of a file by specifying a key. Methods exist to position the cursor, read, update, and delete records by key.

To position the cursor, use the following methods:

To delete a record, use the following method :

The read methods are:

To update a record, use the following method:

Methods are also provided for specifying a search criteria when positioning, reading, and updating by key. Valid search criteria values are as follows:

KeyedFile is a subclass of AS400File; all methods in AS400File are available to KeyedFile.

Specifying the key

The key for a KeyedFile object is represented by an array of Java Objects whose types and order correspond to the types and order of the key fields as specified by the RecordFormat object for the file.

The following example shows how to specify the key for the KeyedFile object.

             // Specify the key for a file whose key fields, in order,
             // are:
             //    CUSTNAME    CHAR(10)
             //    CUSTNUM     BINARY(9)
             //    CUSTADDR    CHAR(100)VARLEN()
             // Note that the last field is a variable-length field.
     Object[] theKey = new Object[3];
     theKey[0] = "John Doe";  
     theKey[1] = new Integer(445123);
     theKey[2] = "2227 John Doe Lane, ANYTOWN, NY 11199";  

A KeyedFile object accepts partial keys as well as complete keys. However, the key field values that are specified must be in order.

For example:

             // Specify a partial key for a file whose key fields,
             // in order, are:
             //    CUSTNAME    CHAR(10)
             //    CUSTNUM     BINARY(9)
             //    CUSTADDR    CHAR(100)VARLEN()
     Object[] partialKey = new Object[2];
     partialKey[0] = "John Doe";  
     partialKey[1] = new Integer(445123);

             // Example of an INVALID partial key
     Object[] INVALIDPartialKey = new Object[2];
     INVALIDPartialKey[0] = new Integer(445123);
     INVALIDPartialKey[1] = "2227 John Doe Lane, ANYTOWN, NY 11199";

Null keys and null key fields are not supported.

The key field values for a record can be obtained from the Record object for a file through the getKeyFields() method.

The following example shows how to read from a file by key:

             // Create an AS400 object, the file exists on this
             // server.
     AS400 sys = new AS400("mySystem.myCompany.com");

             // Create a file object that represents the file
     KeyedFile myFile = new KeyedFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");
             // Assume that the AS400FileRecordDescription class
             // was used to generate the code for a subclass of
             // RecordFormat that represents the record format
             // of file MYFILE in library MYLIB.  The code was
             // compiled and is available for use by the Java program.
     RecordFormat recordFormat = new MYKEYEDFILEFormat();

             // Set the record format for myFile.  This must
             // be done before invoking open()
     myFile.setRecordFormat(recordFormat);

             // Open the file.
     myFile.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);

             // The record format for the file contains
             // four key fields, CUSTNUM, CUSTNAME, PARTNUM
             // and ORDNUM in that order.
             // The partialKey will contain 2 key field
             // values.  Because the key field values must be
             // in order, the partialKey will consist of values for
             // CUSTNUM and CUSTNAME.
     Object[] partialKey = new Object[2];
     partialKey[0] = new Integer(1);
     partialKey[1] = "John Doe";

             // Read the first record matching partialKey
     Record keyedRecord = myFile.read(partialKey);

             // If the record was not found, null is returned.
     if (keyedRecord != null)
     { // Found the record for John Doe, print out the info.
       System.out.println("Information for customer " + (String)partialKey[1] + ":");
       System.out.println(keyedRecord);
     }

                     ....

             // Close the file since I am done using it
     myFile.close();

             // Disconnect since I am done using record-level access
     sys.disconnectService(AS400.RECORDACCESS);