You can use the AS400File class to read, write, update, and delete records in files on the server. The record is accessed through the Record class, which is described by a RecordFormat class. The record format must be set through the setRecordFormat() method before the file is opened, unless the file was just created (without an intervening close()) by one of the create() methods, which sets the record format for the object.
Use the read() methods to read a record from the file. Methods are provided to do the following:
The following example shows how to use the readNext() method:
// 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 SequentialFile myFile = new SequentialFile(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 MYFILEFormat(); // Set the record format for myFile. This must // be done before invoking open() myFile.setRecordFormat(recordFormat); // Open the file. myFile.open(AS400File.READ_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE); // Read each record in the file writing field // CUSTNAME to System.out System.out.println(" CUSTOMER LIST"); System.out.println("____________________________________________"); Record record = myFile.readNext(); while(record != null) { System.out.println(record.getField("CUSTNAME")); record = myFile.readNext(); } .... // Close the file since I am done using it myFile.close(); // Disconnect since I am done using // record-level access. sys.disconnectService(AS400.RECORDACCESS);
Use the update() method to update the record at the cursor position.
For example:
// 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 SequentialFile myFile = new SequentialFile(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 MYFILEFormat(); // Set the record format for myFile. This must // be done prior to invoking open() myFile.setRecordFormat(recordFormat); // Open the file for updating myFile.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE); // Update the first record in the file. Assume // that newName is a String with the new name for // CUSTNAME Record updateRec = myFile.readFirst(); updateRec.setField("CUSTNAME", newName); myFile.update(updateRec); .... // Close the file since I am done using it myFile.close(); // Disconnect since I am done using record-level access sys.disconnectService(AS400.RECORDACCESS);
Use the write() method to append records to the end of a file. A single record or an array of records can be appended to the file.
Use the deleteCurrentRecord() method to delete the record at the cursor position.