Example: Using IFS classes to copy a file from one directory to another
//////////////////////////////////////////////////////////////////////////////////
//
// IFSCopyFile example. This program uses the installable file system classes
// to copy a file from one directory to another on the AS/400.
//
// Command syntax:
// IFSCopyFile system sourceName TargetName
//
// For example,
// IFSCopyFile MySystem /path1/path2/file.ext /path3/path4/path5/file.ext
//
//
//////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of IBM Toolbox for Java "IFSFile".
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind. The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// IBM Toolbox for Java
// (C) Copyright IBM Corp. 1997
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
//////////////////////////////////////////////////////////////////////////////////
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 AS/400 system 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);
}
}