ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/ifslistfilezexample.htm

222 lines
7.8 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using the IFSFile listFiles() method to list the contents of a directory" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="ifslistfilezexample" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using the IFSFile listFiles() method to list the contents
of a directory</title>
</head>
<body id="ifslistfilezexample"><a name="ifslistfilezexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using the IFSFile listFiles() method to list the contents
of a directory</h1>
<div><p></p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
example disclaimer</a> for important legal information.</div>
<pre>//////////////////////////////////////////////////////////////////////////////////
//
// 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 &gt;= 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&lt; 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 &lt;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;
}
}
}</pre>
</div>
</div>
</body>
</html>