IFSRandomAccessFile

The IFSRandomAccessFile class represents a file on the AS/400 for reading and writing data. The Java program can read and write data sequentially or randomly. As in IFSFile, methods exist in IFSRandomAccessFile that duplicate the methods in RandomAccessFile from the java.io package. In addition to these methods, IFSRandomAccessFile has additional methods specific to the AS/400. Through IFSRandomAccessFile, a Java program can do the following:

  • Open a file for read, write, or read/write access. The Java program can optionally specify the file sharing mode and the existence option.
  • Read data at the current offset from the file.
  • Write data at the current offset to the file.
  • Get or set the current offset of the file.
  • Close the file.

The following example shows how to use the IFSRandomAccessFile class to write four bytes at 1K intervals to a file.

                       // Create an AS400 object.
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // Open a file object that represents
                       // the file.
     IFSRandomAccessFile aFile =
                   new IFSRandomAccessFile(sys,"/mydir1/myfile", "rw");

                       // Establish the data to write.
     byte i = 123;

                       // Write to the file 10 times at 1K
                       // intervals.
     for (int j=0; j<10; j++)
     {
                       // Move the current offset.
        aFile.seek(j * 1024);

                       // Write to the file. The current
                       // offset advances by the size of
                       // the write.
        aFile.write(i);
     }

                       // Close the file.
     aFile.close();

In addition to the methods in java.io RandomAccessFile, IFSRandomAccessFile gives the Java program the following options:

  • Committing to disk bytes written.
  • Locking or unlocking bytes in the file.
  • Locking and unlocking bytes in the stream. See IFSKey for more information.
  • Specifying a sharing mode when the file is opened. See sharing modes for more information.
  • Specify the existence option when a file is opened. The Java program can choose one of the following:
    • Open the file if it exists; create the file if it does not.
    • Replace the file if it exists; create the file if it does not.
    • Fail the open if the file exists; create the file if it does not.
    • Open the file if it exists; fail the open if it does not.
    • Replace the file if it exists; fail the open if it does not.


[ Legal | AS/400 Glossary ]