153 lines
6.9 KiB
HTML
153 lines
6.9 KiB
HTML
<?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="Examples: Using IFSFile" />
|
|
<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="ifsfileexamples" />
|
|
<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>Examples: Using IFSFile</title>
|
|
</head>
|
|
<body id="ifsfileexamples"><a name="ifsfileexamples"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Examples: Using IFSFile</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>
|
|
<p>The following
|
|
examples show how to use the IFSFile class:</p>
|
|
<ul><li><a href="#ifsfileexamples__creatingadirectory">Example: Creating a directory</a></li>
|
|
<li><a href="#ifsfileexamples__trackingerrors">Example: Using IFSFile exceptions
|
|
to track errors</a></li>
|
|
<li><a href="#ifsfileexamples__listingtxtfiles">Example: Listing files with
|
|
a .txt extension</a></li>
|
|
<li><a href="ifslistfilezexample.htm#ifslistfilezexample">Example: Using the IFSFile listFiles() method to list the contents of a directory</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="ifsfileexamples__creatingadirectory"><a name="ifsfileexamples__creatingadirectory"><!-- --></a><h4 class="sectiontitle">Example: Creating a directory</h4><pre> // Create an AS400 object. This new
|
|
// directory will be created on this
|
|
// iSeries.
|
|
AS400 sys = new AS400("mySystem.myCompany.com");
|
|
|
|
// Create a file object that
|
|
// represents the directory.
|
|
IFSFile aDirectory = new IFSFile(sys, "/mydir1/mydir2/newdir");
|
|
|
|
// Create the directory.
|
|
if (aDirectory.mkdir())
|
|
System.out.println("Create directory was successful");
|
|
|
|
// Else the create directory failed.
|
|
else
|
|
{
|
|
// If the object already exists,
|
|
// find out if it is a directory or
|
|
// file, then display a message.
|
|
if (aDirectory.exists())
|
|
{
|
|
if (aDirectory.isDirectory())
|
|
System.out.println("Directory already exists");
|
|
else
|
|
System.out.println("File with this name already exists");
|
|
}
|
|
else
|
|
System.out.println("Create directory failed");
|
|
}
|
|
|
|
// Disconnect since I am done
|
|
// accessing files.
|
|
sys.disconnectService(AS400.FILE);</pre>
|
|
</div>
|
|
<div class="section" id="ifsfileexamples__trackingerrors"><a name="ifsfileexamples__trackingerrors"><!-- --></a><h4 class="sectiontitle">Example: Using IFSFile exceptions to track
|
|
errors</h4><p>When an error occurs, the IFSFile class throws the <a href="javadoc/com/ibm/as400/access/ExtendedIOException.html#NAVBAR_TOP"> ExtendedIOException</a> exception. This exception contains
|
|
a return code that indicates the cause of the failure. The IFSFile class throws
|
|
the exception even when the java.io class that IFSFile duplicates does not.
|
|
For example, the delete method from java.io.File returns a boolean to indicate
|
|
success or failure. The delete method in IFSFile returns a boolean, but if
|
|
the delete fails, an ExtendedIOException is thrown. The ExtendedIOException
|
|
provides the Java™ program with detailed information about why the
|
|
delete failed.</p>
|
|
<pre> // Create an AS400 object.
|
|
AS400 sys = new AS400("mySystem.myCompany.com");
|
|
|
|
// Create a file object that
|
|
// represents the file.
|
|
IFSFile aFile = new IFSFile(sys, "/mydir1/mydir2/myfile");
|
|
|
|
// Delete the file.
|
|
try
|
|
{
|
|
aFile.delete();
|
|
|
|
// The delete was successful.
|
|
System.out.println("Delete successful ");
|
|
}
|
|
|
|
// The delete failed. Get the return
|
|
// code out of the exception and
|
|
// display why the delete failed.
|
|
catch (ExtendedIOException e)
|
|
{
|
|
int rc = e.getReturnCode();
|
|
|
|
switch (rc)
|
|
{
|
|
case ExtendedIOException.FILE_IN_USE:
|
|
System.out.println("Delete failed, file is in use ");
|
|
break;
|
|
|
|
case ExtendedIOException.PATH_NOT_FOUND:
|
|
System.out.println("Delete failed, path not found ");
|
|
break;
|
|
|
|
// ... for every specific error
|
|
// you want to track.
|
|
|
|
default:
|
|
System.out.println("Delete failed, rc = ");
|
|
System.out.println(rc);
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="ifsfileexamples__listingtxtfiles"><a name="ifsfileexamples__listingtxtfiles"><!-- --></a><h4 class="sectiontitle">Example: Listing files with a .txt extension</h4><p>The Java program
|
|
can optionally specify match criteria when listing files in the directory.
|
|
Match criteria reduce the number of files that are returned by the server
|
|
to the IFSFile object, which improves performance. The following example
|
|
shows how to list files with extension .txt:</p>
|
|
<pre> // Create the AS400 object.
|
|
AS400 system = new AS400("mySystem.myCompany.com");
|
|
|
|
// Create the file object.
|
|
IFSFile directory = new IFSFile(system, "/");
|
|
|
|
// Generate a list of all files with
|
|
// extension .txt
|
|
String[] names = directory.list("*.txt");
|
|
|
|
// Display the names.
|
|
if (names != null)
|
|
for (int i = 0; i < names.length; i++)
|
|
System.out.println(names[i]);
|
|
else
|
|
System.out.println("No .txt files");</pre>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |