Creating and deleting files and members

Physical files on the server are created by specifying a record length, an existing server data description specifications (DDS) source file, or a RecordFormat object.

When you create a file and specify a record length, a data file or a source file can be created. The method sets the record format for the object. Do not call the setRecordFormat() method for the object.

A data file has one field. The field name is the name of the file, the field type is of type character, and the field length is the length that is specified on the create method.

A source file has three fields:

The following examples show how to create files and members.

Example 1: To create a data file with a 128-byte record:

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

                       // Create a file object that represents the file
     SequentialFile newFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");

                       // Create the file
     newFile.create(128, "*DATA", "Data file with a 128 byte record");

                       // Open the file for writing only.
                       // Note: The record format for the file
                       // has already been set by create()
     newFile.open(AS400File.WRITE_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);

                       // Write a record to the file.  Because the record
                       // format was set on the create(), getRecordFormat()
                       // can be called to get a record properly formatted
                       // for this file.
     Record writeRec = newFile.getRecordFormat().getNewRecord();
     writeRec.setField(0, "Record one");
     newFile.write(writeRec);

                     ....

                       // Close the file since I am done using it
     newFile.close();
                       // Disconnect since I am done using
                       // record-level access
     sys.disconnectService(AS400.RECORDACCESS);

Example 2: When creating a file specifying an existing DDS source file, the DDS source file is specified on the create() method. The record format for the file must be set using the setRecordFormat() method before the file can be opened. For example:

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

                       // Create QSYSObjectPathName objects for
                       // both the new file and the DDS file.
     QSYSObjectPathName file    = new QSYSObjectPathName("MYLIB", "MYFILE", "FILE", "MBR");
     QSYSObjectPathName ddsFile = new QSYSObjectPathName("MYLIB", "DDSFILE", "FILE", "MBR");

                       // Create a file object that represents the file
     SequentialFile newFile = new SequentialFile(sys, file);

                       // Create the file
     newFile.create(ddsFile,  "File created using DDSFile description");

                       // Set the record format for the file 
                       // by retrieving it from the server.
     newFile.setRecordFormat(new AS400FileRecordDescription(sys,
     newFile.getPath()).retrieveRecordFormat()[0]);

                       // Open the file for writing
     newFile.open(AS400File.WRITE_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);

                       // Write a record to the file.  The getRecordFormat()
                       // method followed by the getNewRecord() method is used to get
               // a default record for the file.
     Record writeRec = newFile.getRecordFormat().getNewRecord();
     newFile.write(writeRec);

                     ....

                       // Close the file since I am done using it
     newFile.close();
                       // Disconnect since I am done using
                       // record-level access
     sys.disconnectService(AS400.RECORDACCESS);

Example 3: When creating a file specifying a RecordFormat object, the RecordFormat object is specified on the create() method. The method sets the record format for the object. The setRecordFormat() method must not be called for the object.

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

                       // Create a file object that represents the file
     SequentialFile newFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");

                      // Retrieve the record format from an existing file
     RecordFormat recordFormat = new AS400FileRecordDescription(sys,
     "/QSYS.LIB/MYLIB.LIB/EXISTING.FILE/MBR1.MBR").retrieveRecordFormat()[0];

                       // Create the file
     newFile.create(recordFormat,  "File created using record format object");

                       // Open the file for writing only.
                       // Note: The record format for the file
                       // has already been set by create()
     newFile.open(AS400File.WRITE_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);

                       // Write a record to the file.  The recordFormat
                       // object is used to get a default record
                       // properly formatted for the file.
     Record writeRec = recordFormat.getNewRecord();
     newFile.write(writeRec);

                     ....

                       // Close the file since I am done using it
     newFile.close();
                       // Disconnect since I am done using
                       // record-level access
     sys.disconnectService(AS400.RECORDACCESS);

When deleting files and members, use these methods:

Use the addPhysicalFileMember() method to add members to a file.