////////////////////////////////////////////////////////////////////////////////// // // IFSListFiles example. This program uses the integrated file system // classes to list the contents of a directory on the server. // // Command syntax: // IFSListFiles system directory // // For example, // IFSListFiles MySystem /path1 // ////////////////////////////////////////////////////////////////////////////////// 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 server 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 in the following code, // no call to the server is required. // // However, with the user of the listFiles method, attribute // information will not be refreshed automatically from the // server. This means attribute information can become // inconsistent with information about the server. IFSFile[] directoryFiles = directory.listFiles(new MyDirectoryFilter(),"*"); // 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 MyDirectoryFilter 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; } } }