Using IBM Toolbox for Java to access the integrated file system

This article introduces the use of IBM i Toolbox for Java to access integrated file system files and directories. This article shows how to use integrated file system classes provided by IBM Toolbox for Java to manage files in the integrated file system.

Share:

Meng Su (mengsu@cn.ibm.com), Software Engineer, IBM China

Meng Su photoMeng Su is a software engineer for IBM China system and technology laboratory, where she focuses on the test of IBM Systems Director Navigator for i.



Xing Fei Yang (yxingfei@cn.ibm.com), Staff Software Engineer, IBM China

Xing Yang photoXing Fei Yang is a staff software engineer for IBM China system and technology laboratory, where he focuses on the development of IBM Systems Director Navigator for i.



22 November 2011

Integrated File System

The integrated file system is a part of IBM i. It provides an integrating structure over all information stored in your system. It supports stream input/output and storage management.

There are many different file systems that exist on IBM i. The integrated file system provides a common interface to access objects in different file systems. IBM Toolbox for Java basically uses the integrated file system interfaces and provides a set of Java classes to access integrated file system objects. It's very convenient for Java programs to access integrated file system objects.


IBM Toolbox for Java

The IBM Toolbox for Java/JTOpen is a library of Java classes supporting the client/server and internet programming models to IBM i. The classes can be used by Java applets, servlets, and applications to easily access IBM i data and resources.

JTOpen is the open source version of the IBM Toolbox for Java licensed program product, and contains the identical code. Both have been tested on a wide variety of platforms, including AIX, IBM i, Linux, Solaris, and Windows.

AS400 class of IBM Toolbox for Java

The AS400 class represents the authentication information and a set of connections to the IBM i system. Before accessing files in the integrated file system, you need to establish a connection to the IBM i system using an instance of AS400 class. The following sample code illustrates creating an AS400 object.

	AS400 obj = new AS400(systemName, userId, password)

The parameter systemName is the host name or IP address of the IBM i system. The parameters userId and password are the user profile name and password which are used to authenticate to the IBM i system.

Integrated File System classes of IBM Toolbox for Java

IBM Toolbox for Java has a set of classes to access integrated file system files and directories. The usages of them are similar to the classes in java.io package.

The table below lists the main classes which are used to access integrated file system files and directories.

Class NameDescription
IFSFile The IFSFile class represents an object in the integrated file system. You can use IFSFile to delete or rename a file, to access the file's attributes or to list the contents of a directory.
IFSFileInputStream The IFSFileInputStream class represents an integrated file system input stream.
IFSFileOutputStream The IFSFileOutputStream class represents an integrated file system file output stream.
IFSReader Convenience class for reading character files in the integrated file system.
IFSWriter Convenience class for writing character files in the integrated file system.
IFSRandomAccessFile The IFSRandomAccessFile class supports read and write access to integrated file system objects.

Getting started

To start using IBM Toolbox for Java, you need to configure your project with IBM Toolbox for Java API. The procedure is as follows:

  1. Download the IBM Toolbox for Java jar files
  2. Extract the zip file
  3. Put the jt400.jar into your project classpath.

Reading files

You can use class IFSFileInputStream to read the files in the integrated file system. The IFSFileInputStream class represents an integrated file system input stream. The behavior of this class is similar to java.io.FileInputStream.

The following sample code illustrates reading the integrated file system files with class IFSFileInputStream.

 /**
   Reads an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @return The characters read.
   @throws IOException
   @throws AS400SecurityException
   **/
   public byte[] readFile(AS400 system, String path) throws AS400SecurityException,
 IOException{
      IFSFile file = new IFSFile(system, path);
      /** creates a file input stream */
      IFSFileInputStream fis = new IFSFileInputStream (file,
      IFSFileInputStream.SHARE_READERS);
      byte[] data = new byte[fis.available()];
      fis.read(data);
      fis.close();
      return data;
 }

You can also use class IFSFileReader to read files. IFSFileReader is meant for reading streams of characters. You can use it to read character files in the integrated file system. The behavior of this class is similar to java.io.FileReader.

The following sample code illustrates reading files with class IFSFileReader.

 /**
   Reads an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @return The characters read.
   @throws IOException
   @throws AS400SecurityException
 **/
   public String readFile(AS400 system, String path) throws AS400SecurityException,
 IOException{
      IFSFile file = new IFSFile(system, path);
      /** creates a file reader */
      IFSFileReader fr = new IFSFileReader (file);
      BufferedReader br = new BufferedReader(fr);
      StringBuffer sb = new StringBuffer();
      /** Reads a line of text */
      String line = br.readLine();
      while (line != null){
           line = br.readLine();
           sb.append(line);
      }
      fr.close();
      return sb.toString();
 }

Writing files

You can use class IFSFileOutputStream to write the files in the integrated file system. The IFSFileOutputStream class represents an integrated file system output stream. The behavior of this class is similar to java.io.FileOutputStream.

The following sample code illustrates writing the files in the integrated file system with class IFSFileOutputStream.

 /**
   Writes an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @param bytes The bytes to be written.
   @throws IOException
   @throws AS400SecurityException
 **/
   public void writeFile(AS400 system, String path, byte[] bytes) throws
 AS400SecurityException, IOException {
      IFSFile file = new IFSFile(system, path);
      /** creates a file output stream */
      IFSFileOutputStream fos = new IFSFileOutputStream (file);
      /** Writes bytes to file */
      fos.write(bytes);
      fos.close();
 }

You can also use class IFSWriter to write the files in the integrated file system. IFSWriter is meant for writing streams of characters. You can use it to write character streams to files in the integrated file system. The behavior of this class is similar to java.io.FileWriter.

The following sample code illustrates writing integrated file system files with class IFSFileWriter.

 /**
   Writes an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @param str The characters to be written.
   @throws IOException
   @throws AS400SecurityException
 **/
   public void writeFile(AS400 system, String path, String str) throws IOException,
 AS400SecurityException {
      IFSFile file = new IFSFile(system, path);
      /** creates a file writer */
      IFSFileWriter fw = new IFSFileWriter (file);
      /** Writes a string to file */
      fw.write(str);
      fw.close();
 }

Creating new directories

You can use class IFSFile to create directories in the integrated file system.

The following sample code illustrates creating an integrated file system directory with class IFSFile.

 /**
   Creates a new directory
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @throws IOException
 **/
   public void createDir(AS400 system, String path) throws IOException{
      IFSFile dir = new IFSFile (system, path);
      if (!dir.exists()){
         dir.mkdir();
   }
 }

Creating new files

You can use class IFSFile to create a new, empty file.

The following sample code illustrates creating an integrated file system file with class IFSFile.

 /**
   Creates a new file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @throws IOException
 **/
   public void createFile(AS400 system, String path) throws IOException{
         IFSFile file = new IFSFile(system, path);
         if (!file.exists()){
         file.createNewFile();
   }
 }

Deleting files or directories

You can use class IFSFile to delete directories or files in the integrated file system.

The following sample code illustrates deleting a directory/file with class IFSFile.

 /**
   Deletes a directory or file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @throws IOException
 **/
   public void delete(AS400 system, String path) throws IOException{
      IFSFile file = new IFSFile(system, path);
      if (file.exists()){
      file.delete();
   }
 }

Copying files or directories

You can use class IFSFile to copy an integrated file system directory or file to the specified directory or file.

Here is a sample.

 /**
   Copy integrated file system file or directory
   @param system The system that contains the file.
   @param srcPath The source path.
   @param destpath The destination path.
   @throws ObjectAlreadyExistsException.
   @throws AS400SecurityException
   @throws AS400SecurityException
 **/
   public void copy(AS400 system, String srcPath, String destPath
   ) throws IOException, AS400SecurityException, ObjectAlreadyExistsException{
      IFSFile file = new IFSFile(system, srcPath);
      file.copyTo(destPath);
   }

File CCSID handling with IBM Toolbox for Java

You can use class IFSFileWriter to write a character file with the specific CCSID.

The following sample code illustrates writing character files with the specific CCSID.

 /**
   Writes an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @param chars The characters to be written.
   @param CCSID The CCSID to convert the data to when writing to the file.
   @throws IOException
   @throws AS400SecurityException
 **/
   public void writeFile(AS400 system, String path, char[] chars, int CCSID ) throws
 AS400SecurityException, IOException  {
      IFSFile file = new IFSFile(system, path);
      /** creates a file writer */
      IFSFileWriter fw = new IFSFileWriter (file, CCSID);
      /** Writes a string to file */
      fw.write(chars);
      fw.close();
 }

You can use class IFSFileReader to read a character file with the specific CCSID.

The following sample code illustrates reading files with the specific CCSID.

 /**
   Reads an integrated file system file.
   @param system The system that contains the file.
   @param path The absolute path name of the file.
   @param The CCSID that the file data is currently in.
   @throws IOException
   @throws AS400SecurityException
   @return The characters read.
 **/
   public String readFile(AS400 system, String path, int CCSID) throws
 AS400SecurityException, IOException {
   IFSFile file = new IFSFile(system, path);
   /** creates a file reader */
   IFSFileReader fr = new IFSFileReader (file, CCSID);
   BufferedReader br = new BufferedReader(fr);
   StringBuffer sb = new StringBuffer();
   /** Reads a line of text */
   String line = br.readLine();
   while (line != null){
             line = br.readLine();
             sb.append(line);
   }
   fr.close();
   return sb.toString();
 }

Using buffer to improve file reading/writing performance

You can create an instance of java.io.BufferedInputStream using IFSFileInputStream. The BufferedInputStream has an internal buffer array to buffer the input stream. So it can reduce the time of accessing IO and improve the performance. You can also create an instance of java.io.BufferedOutputStream using IFSFileOutputStream to improve writing performance. Likewise, you can also create an instance of java.io.BufferedReader for IFSFileReader or create an instance of java.io.BufferedWriter for IFSFileWriter.


Summary

In this article, we introduced the concept of the integrated file system and IBM Toolbox for Java. We showed how to utilize IBM Toolbox for Java methods to access and manipulate integrated file system files and directories. The examples demonstrated how easy it is to incorporate integrated file system files and directories logic into Toolbox applications.


Resources

Here are some useful resources you can refer to for some detailed information about the integrated file system and IBM Toolbox for Java/JTOpen.

  1. IBM i Information Center topic on Integrated file system classes
  2. Read the developerWorks article on Toolbox for Java and JTOpen
  3. Download IBM Toolbox for Java/JTOpen zip file
  4. Refer to IBM Toolbox for Java and JTOpen for more information

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into IBM i on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=IBM i
ArticleID=775581
ArticleTitle=Using IBM Toolbox for Java to access the integrated file system
publish-date=11222011