[ Previous part | Simple programming examples ]
Use the following as an example for your program. The example includes detailed explanations about key lines in the code. You can view the detailed explanations in the following ways:
Note: Read the Code example disclaimer for important legal information.
////////////////////////////////////////////////////////////////////////////////// // // Record level access example. // // Calling syntax: java RLACreateExample // ////////////////////////////////////////////////////////////////////////////////// import java.io.*; import java.util.*; import com.ibm.as400.access.*; public class RLACreateExample { public static void main(String[] args) { AS400 system = new AS400(args[0]); String filePathName = "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MBR1.MBR"; Note 1try { SequentialFile theFile = new SequentialFile(system, filePathName); // Begin Note Two CharacterFieldDescription lastNameField = new CharacterFieldDescription(new AS400Text(20), "LNAME"); CharacterFieldDescription firstNameField = new CharacterFieldDescription(new AS400Text(20), "FNAME"); BinaryFieldDescription yearsOld = new BinaryFieldDescription(new AS400Bin4(), "AGE"); RecordFormat fileFormat = new RecordFormat("RF"); fileFormat.addFieldDescription(lastNameField); fileFormat.addFieldDescription(firstNameField); fileFormat.addFieldDescription(yearsOld); theFile.create(fileFormat, "A file of names and ages"); Note 2
// End Note Two theFile.open(AS400File.READ_WRITE, 1, AS400File.COMMIT_LOCK_LEVEL_NONE); // Begin Note Three Record newData = fileFormat.getNewRecord(); newData.setField("LNAME", "Doe"); newData.setField("FNAME", "John"); newData.setField("AGE", new Integer(63)); theFile.write(newData); Note 3
// End Note Three theFile.close(); } catch(Exception e) { System.out.println("An error has occurred: "); e.printStackTrace(); } system.disconnectService(AS400.RECORDACCESS); System.exit(0); } }
(args[0]) in the previous line and MYFILE.FILE are pieces of code that are prerequisites for the rest of the example to run. The program assumes that the library MYLIB exists on the server and that the user has access to it.
The text within the Java comments labeled "Begin Note Two" and "End Note Two" shows how to create a record format yourself instead of getting the record format from an existing file. The last line in this block creates the file on the server.
The text within the Java comments labeled "Begin Note Three" and "End Note Three" shows a way to create a record and then write it to a file.