[Previous Example | Main Tutorial Page]

RLA: Example 2 of 2

Use the following as an example for your program.


import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;

public class RLACreateExample
{
  public static void main(String[] args)
  {
    AS400 system = new AS400(args[0]);
    String filePathName = "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MBR1.MBR";1 Click to display a detailed explanation

    try
    {
      SequentialFile theFile = new SequentialFile(system, filePathName);
	  
	  
      CharacterFieldDescription lastNameField = new CharacterFieldDescription(new AS400Text(20), "LNAME");
      CharacterFieldDescription firstNameField = new CharacterFieldDescription(new AS400Text(20), "FNAME");
      BinaryFieldDescription yearsOld = new BinaryFieldDescription(new AS400Bin4(), "AGE");

      RecordFormat fileFormat = new RecordFormat("RF");
      fileFormat.addFieldDescription(lastNameField);
      fileFormat.addFieldDescription(firstNameField);
      fileFormat.addFieldDescription(yearsOld);

      theFile.create(fileFormat, "A file of names and ages");2 Click to display a detailed explanation


      theFile.open(AS400File.READ_WRITE, 1, AS400File.COMMIT_LOCK_LEVEL_NONE);

	  
      Record newData = fileFormat.getNewRecord();
      newData.setField("LNAME", "Doe");
      newData.setField("FNAME", "John");
      newData.setField("AGE", new Integer(63));

      theFile.write(newData);3 Click to display a detailed explanation
      
      theFile.close();
    }
    catch(Exception e)
    {
      System.out.println("An error has occurred: ");
      e.printStackTrace();
    }

    system.disconnectService(AS400.RECORDACCESS);

    System.exit(0);
  }
}

  1. The line portions in blue are pieces of code that are prerequisites for the rest of the example to run. The program assumes that the library MYLIB exists on the AS/400 and that the user has access to it.

  2. The text in red shows you how to create a record format yourself instead of getting the record format from an existing file. The last line in this block creates the file on the AS/400.

  3. The green text shows a way to create a record and then write it to a file.




Previous