Example: Using the IFSFile listFiles() method to list the contents of a directory
////////////////////////////////////////////////////////////////////////
//
// IFSListFiles example. This program uses the integrated file system
// classes to list the contents of a directory on the AS/400.
//
// Command syntax:
// IFSListFiles system directory
//
// For example,
// IFSListFiles MySystem /path1
//
//
//////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of AS/400 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.
//
// AS/400 Toolbox for Java
// (C) Copyright IBM Corp. 2000
// 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 IFSListFiles extends Object
{
public static void main(String[] parameters)
{
System.out.println( " " );
String directoryName = "";
String system = "";
// if both parameters were not specified, display help text and exit.
if (parameters.length >= 2)
{
// Assume the first parameter is the system name and
// the second parameter is the directory name
system = parameters[0];
directoryName = parameters[1];
try
{
// Create an AS400 object for the AS/400 system that holds the files.
AS400 as400 = new AS400(system);
// Create the IFSFile object for the directory.
IFSFile directory = new IFSFile(as400, directoryName);
// Generate a list of IFSFiles. Pass the listFiles method
// the directory filter object and the search match
// criteria. This method caches attribute information. For
// instance, when isDirectory() is called on an IFSFile
// object in the file array returned below, no call to the
// AS/400 is required.
//
// However, with the user of the listFiles method, attribute
// information will not be automatically refreshed from the
// AS/400. This means attribute information can
// become inconsistent with the AS/400.
IFSFile[] directoryFiles = directory.listFiles(new directoryFilter(),"*");
// Tell the user if the directory doesn't exist or is empty
if (directoryFiles == null)
{
System.out.println("The directory does not exist");
return;
}
else if (directoryFiles.length == 0)
{
System.out.println("The directory is empty");
return;
}
for (int i=0; i< directoryFiles.length; i++)
{
// Print out information on list.
// Print the name of the current file
System.out.print(directoryFiles[i].getName());
// Pad the output so the columns line up
for (int j = directoryFiles[i].getName().length(); j <18; j++)
System.out.print(" ");
// Print the date the file was last changed.
long changeDate = directoryFiles[i].lastModified();
Date d = new Date(changeDate);
System.out.print(d);
System.out.print(" ");
// Print if the entry is a file or directory
System.out.print(" ");
if (directoryFiles[i].isDirectory())
System.out.println("");
else
System.out.println(directoryFiles[i].length());
}
}
catch (Exception e)
{
// If any of the above operations failed say the list failed
// and output the exception.
System.out.println("List 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(" IFSListFiles as400 directory");
System.out.println("");
System.out.println("Where");
System.out.println("");
System.out.println(" as400 = system that contains the files");
System.out.println(" directory = directory to be listed");
System.out.println("");
System.out.println("For example:");
System.out.println("");
System.out.println(" IFSListFiles mySystem /dir1/dir2");
System.out.println("");
System.out.println("");
}
System.exit(0);
}
}
////////////////////////////////////////////////////////////////////////////
//
// The directory filter class prints information from the file object.
//
// Another way to use the filter is to simply return true or false
// based on information in the file object. This lets the mainline
// function decide what to do with the list of files that meet the
// search criteria.
//
////////////////////////////////////////////////////////////////////////////
class directoryFilter implements IFSFileFilter
{
public boolean accept(IFSFile file)
{
try
{
// Keep this entry. Returning true tells the IFSList object
// to return this file in the list of entries returned to the
// .list() method.
return true;
}
catch (Exception e)
{
return false;
}
}
}