The AS400FileRecordDescription class provides the methods for retrieving the record format of a file on the server. This class provides methods for creating Java™ source code for subclasses of RecordFormat and for returning RecordFormat objects, which describe the record formats of user-specified physical or logical files on the server. The output of these methods can be used as input to an AS400File object when setting the record format.
It is recommended that the AS400FileRecordDescription class always be used to generate the RecordFormat object when the file already exists on the server.
Creating Java source code for subclasses of RecordFormat to represent the record format of files on the server
The createRecordFormatSource() method creates Java source files for subclasses of the RecordFormat class. The files can be compiled and used by an application or applet as input to the AS400File.setRecordFormat() method.
The createRecordFormatSource() method should be used as a development time tool to retrieve the record formats of existing files on the server. This method allows the source for the subclass of the RecordFormat class to be created once, modified if necessary, compiled, and then used by many Java programs accessing the same files on the server. Because this method creates files on the local system, it can be used only by Java applications. The output (the Java source code), however, can be compiled and then used by Java applications and applets alike.
Example 1: The following example shows how to use the createRecordFormatSource() method:
// Create an AS400 object, the file exists on this // server. AS400 sys = new AS400("mySystem.myCompany.com"); // Create an AS400FileRecordDescription object that represents the file AS400FileRecordDescription myFile = new AS400FileRecordDescription(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE"); // Create the Java source file in the current working directory. // Specify "package com.myCompany.myProduct;" for the // package statement in the source since I will ship the class // as part of my product. myFile.createRecordFormatSource(null, "com.myCompany.myProduct"); // Assuming that the format name for file MYFILE is FILE1, the // file FILE1Format.java will be created in the current working directory. // It will overwrite any file by the same name. The name of the class // will be FILE1Format. The class will extend from RecordFormat.
Example 2: Compile the file you created above, FILE1Format.java, and use it as follows:
// Create an AS400 object, the file exists on this // server. AS400 sys = new AS400("mySystem.myCompany.com"); // Create an AS400File object that represents the file SequentialFile myFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE"); // Set the record format // This assumes that import.com.myCompany.myProduct.FILE1Format; // has been done. myFile.setRecordFormat(new FILE1Format()); // Open the file and read from it .... // Close the file since I am done using it myFile.close(); // Disconnect since I am done using record-level access sys.disconnectService(AS400.RECORDACCESS);Creating RecordFormat objects to represent the record format of files on the server
The retrieveRecordFormat() method returns an array of RecordFormat objects that represent the record formats of an existing file on the server. Typically, only one RecordFormat object is returned in the array. When the file for which the record format is being retrieved is a multiple format logical file, more than one RecordFormat object is returned. Use this method to dynamically retrieve the record format of an existing file on the server during runtime. The RecordFormat object then can be used as input to the AS400File.setRecordFormat() method.
The following example shows how to use the retrieveRecordFormat() method:
// Create an AS400 object, the file exists on this // server. AS400 sys = new AS400("mySystem.myCompany.com"); // Create an AS400FileRecordDescription object that represents the file AS400FileRecordDescription myFile = new AS400FileRecordDescription(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE"); // Retrieve the record format for the file RecordFormat[] format = myFile.retrieveRecordFormat(); // Create an AS400File object that represents the file SequentialFile myFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE"); // Set the record format myFile.setRecordFormat(format[0]); // Open the file and read from it .... // Close the file since I am done using it myFile.close(); // Disconnect since I am done using record-level access sys.disconnectService(AS400.RECORDACCESS);