Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Sequential data processing

A simple copy program for SAM files is used to demonstrate sequential data processing. The program requires two parameters: the name of the file which is to be copied and the file name of the copy. If the target file already exists, it is deleted (take care!) and created again.

The example is so designed that no knowledge of the file attributes, such as record format or record length, of the file to be copied is required. The error handling in the example is not particularly convenient, simply to prevent the comprehensive code which would be required for this distracting the reader from the way the interface is actually used.

The program is contained in the CopySAM.java file:

import java.io.*;
import com.fujitsu.ts.jrio.*;
/**
 * This sample program demonstrates the use of the
 * JRIO interfaces for file handling and sequential
 * input and output.
 *
 * The program creates a copy of a DMS file of type SAM
 * by sequentially copying each record of the file.
 *
 * The interesting part of this program is the method
 * doCopySAM(), all other methods are added to make it
 * a complete executable program.
 */
public class CopySAM
{
    /**
     * The main method, which analyses the program arguments,
     * calls the work method and provides global error
     * handling.
    */
   public static void main(String args[])
   {
       String source = null;
       String target = null;
       for (int i = 0; i < args.length; i++)
       {
           if (source == null)
               source = args[i];
           else if (target == null)
               target = args[i];
           else
               usage();
       }
       if (source == null || target == null)
           usage();
       try {
           doCopySAM(source,target);
       } catch (Exception e) {
           error(e.toString());
       }
   }
   /**
    * Print a usage message and exit with error
    */
   private static void usage()
   {
       error("Usage: CopySAM source target");
   }
   /**
    * Print the given error message and exit
    */
   private static void error(String msg)
   {
       System.err.println(msg);
       System.exit(1);
   }
   /**
   * The work method.
   * This method demonstrates, how the JRIO interfaces
   * may be used to copy a complete SAM file by
   * sequential read and write operations.
   *
   * @param   source
   *          The name of the file to be copied
   * @param   target
   *          The name of the copied file
   */
   public static void doCopySAM(String source,String target)
       throws IOException
   {
       Record rec;
       RecordFile sourceFile;
       RecordFile targetFile;
       FileInputRecordStream input;
       FileOutputRecordStream output;
       /**
        * check file names and create RecordFile objects
        */
       sourceFile = new RecordFile(source,"DMS");
       targetFile = new RecordFile(target,"DMS");
       /**
        * check source file existence 
        */
       if (!sourceFile.exists())
         error("Source file " + source + " does not exist");
       /**
        * check target file existence 
        */
       if (targetFile.exists())
       {
           /**
           * delete the existing file
           */
           if (!targetFile.delete())
           error("Target file " + target
                + " could not be deleted");
   }
   /**
    * create an empty output file with same attributes
    */
   if (!targetFile.createNewFile(
            sourceFile.getAccessParameter("SAM")))
    error("Target file " + target + " still exists");
   /**
    * open source for input 
    */
   input = new FileInputRecordStream(sourceFile,"SAM");
   /**
    * open target for output
    */
   output =new FileOutputRecordStream(targetFile,"SAM");
   /**
    * read and write all records
    */
   while ((rec = input.read()) != null)
         output.write(rec);
    /**
     * close all files
    */
    input.close();
    output.close();
   }
}

The program can be compiled with the Java compiler javac and then be run. No particular specifications are required to make the JRIO interfaces available.