Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

Sequentielle Datenverarbeitung

&pagelevel(4)&pagelevel

Zur Demonstration der sequentiellen Dateibearbeitung wird ein einfaches Kopierprogramm für SAM-Dateien herangezogen. Das Programm erwartet zwei Parameter, den Namen der Datei, die zu kopieren ist, und den Dateinamen der Kopie. Existiert die Zieldatei schon, wird sie gelöscht (Vorsicht!) und neu angelegt.

Das Beispiel ist so angelegt, dass man keine Kenntnisse über die Dateiattribute, wie Satzformat oder Satzlänge, der zu kopierenden Datei benötigt. Die Fehlerbehandlung ist im Beispiel nicht besonders komfortabel ausgelegt, damit der dafür notwendige umfangreiche Code nicht von der eigentlichen Schnittstellennutzung ablenkt.

Das Programm befindet sich in der Datei CopySAM.java:

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();
   }
}

Das Programm kann mit dem Java-Compiler javac übersetzt und anschließend zum Ablauf gebracht werden. Es sind dabei keine besonderen Angaben notwendig, um die JRIO-Schnittstellen verfügbar zu haben.