Example: Using IFS classes to copy a file from one directory to another

Note: Read the Code example disclaimer for important legal information.
//////////////////////////////////////////////////////////////////////////////////
//
// IFSCopyFile example.  This program uses the installable file system classes
// to copy a file from one directory to another on the server.
//
// Command syntax:
//    IFSCopyFile system sourceName TargetName
//
// For example,
//    IFSCopyFile MySystem  /path1/path2/file.ext  /path3/path4/path5/file.ext
//
//////////////////////////////////////////////////////////////////////////////////



import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;

public class IFSCopyFile extends Object
{
   public static void main(String[] parameters)
   {
      System.out.println( " " );

      String sourceName = "";
      String targetName = "";
      String system     = "";
      byte[] buffer     = new byte[1024 * 64];

      IFSFileInputStream  source = null;
      IFSFileOutputStream target = null;

      // if all three parameters were not specified, display help text and exit.

      if (parameters.length > 2)
      {

         // Assume the first parameter is the system name,
         // the second parameter is the source name and
         // the third parameter is the target name.

         system     = parameters[0];
         sourceName = parameters[1];
         targetName = parameters[2];

         try
         {
            // Create an AS400 object for the server that holds the files.

            AS400 as400 = new AS400(system);



            // Open the source file for exclusive access.

            source = new IFSFileInputStream(as400, sourceName, IFSFileInputStream.SHARE_NONE);

            System.out.println("Source file successfully opened");



            // Open the target file for exclusive access.

            target = new IFSFileOutputStream(as400, targetName, IFSFileOutputStream.SHARE_NONE, false);

            System.out.println("Target file successfully opened");



            // Read the first 64K bytes from the source file.

            int bytesRead = source.read(buffer);



            // While there is data in the source file copy the data from
            // the source file to the target file.

            while (bytesRead > 0)
            {
               target.write(buffer, 0, bytesRead);
               bytesRead = source.read(buffer);
            }

            System.out.println("Data successfully copied");



            // Clean up by closing the source and target files.

            source.close();
            target.close();



            // Get the last changed date/time from the source file and
            // set it on the target file.

            IFSFile src = new IFSFile(as400, sourceName);
            long dateTime = src.lastModified();

            IFSFile tgt = new IFSFile(as400, targetName);
            tgt.setLastModified(dateTime);

            System.out.println("Date/Time successfully set on target file");
            System.out.println("Copy Successful");

         }
         catch (Exception e)
         {
            // If any of the above operations failed say the copy failed
            // and output the exception.

            System.out.println("Copy failed");
            System.out.println(e);
         }
      }

      // Display help text when parameters are incorrect.

      else
      {
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("Parameters are not correct.  Command syntax is:");
         System.out.println("");
         System.out.println("   IFSCopyFile as400 source target");
         System.out.println("");
         System.out.println("Where");
         System.out.println("");
         System.out.println("   as400  = system that contains the files");
         System.out.println("   source = source file in /path/path/name format");
         System.out.println("   target = target file in /path/path/name format");
         System.out.println("");
         System.out.println("For example:");
         System.out.println("");
         System.out.println("   IFSCopyFile myAS400  /dir1/dir2/a.txt  /dir3/b.txt");
         System.out.println("");
         System.out.println("");
      }

      System.exit(0);
   }
}