Copying spooled files

You can use the copy method of the SpooledFile class to create a copy of the spooled file that the SpooledFile object represents.

Using SpooledFile.copy() performs the following actions:

  • Creates the new spooled file on the same output queue and on the same system as the original spooled file
  • Returns a reference to the new spooled file
SpooledFile.copy() is a new method available to you only if you download JTOpen 3.2 or later or apply an IBM® i fix. It is recommended that the better solution is to download and use JTOpen. For more information, see the following:

The copy method uses the Create Spooled File (QSPCRTSP) API within the network print server job to create an exact replica of the spooled file. You need only a unique creation date and time to preserve the identity of the newly created copy of the spooled file.

Specifying an output queue as a parameter to the copy method creates the copy of the spooled file to the first position on the specified output queue. Both the output queue and the original spooled file must reside on the same system

Example: Copying a spooled file using SpooledFile.copy()

Note: Read the Code example disclaimer for important legal information.

This example shows how to use SpooledFile.copy() to copy a spooled file to the same queue that contains the file you want to copy. When you want to route the newly copied spooled file to a specific output queue, pass the output queue as a parameter to the copy method:

     SpooledFile newSplf = new sourceSpooledFile.copy(<outqname>);

where <outqname> is the OutputQueue object.

     public static void main(String args[]) {
        // Create the system object
        AS400 as400 = new AS400(<systemname>,<username>, <password>);
        // Identify the output queue that contains the spooled file you want to copy.
        OutputQueue outputQueue =
          new OutputQueue(as400, "/QSYS.LIB/QUSRSYS.LIB/<outqname>.OUTQ");

        // Create an array that contains all the elements required to
        // uniquely identify a spooled file on the server.
        String[][] splfTags = { {
           <spoolfilename>,
           <spoolfilenum>,
           <jobname>,
           <username>,
           <jobnumber>,
           // Note that <systemname>,<date>, and <time> are optional.
           // If you do not include them, remove the corresponding
           // splfTags[i],[j], where j has the value of 5,6, or 7.
           <systemname>,
           <date>,
           <time>},
        };

        // Print the information that identifies the spooled file to System.out
        for ( int i=0; i<splfTags.length; i++) {
           System.out.println("Copying -> " + splfTags[i][0] + ","
                                            + splfTags[i][1] + ","
                                            + splfTags[i][2] + ","
                                            + splfTags[i][3] + ","
                                            + splfTags[i][4] + ","
                                            + splfTags[i][5] + ","
                                            + splfTags[i][6] + ","
                                            + splfTags[i][7] );

        // Create the SpooledFile object for the source spooled file.
           SpooledFile sourceSpooledFile =
              new SpooledFile(as400,
                              splfTags[i][0],
                              Integer.parseInt(splfTags[i][1]),
                              splfTags[i][2],
                              splfTags[i][3],
                              splfTags[i][5],
                              splfTags[i][6],
                              splfTags[i][7] );
        }

        // Copy the spooled file, which creates a new SpooledFile object.
        // To route the copy of the spooled file to a specific output queue,
        // use the following code:
        // SpooledFile newSplf = new sourceSpooledFile.copy(<outqname>);
        // where <outqname> is an OutputQueue object. Specify the output
        // queue in the following way:
        // OutputQueue outputQueue = 
        //    new OutputQueue(as400, "/QSYS.LIB/QUSRSYS.LIB/<outqname>.OUTQ");
        try { SpooledFile newSplf = new sourceSpooledFile.copy();
        }

        catch ( Exception e){
        }