File I/O

If you just need to save lines of text to file and be able to read them, you can achieve this fairly simply with a PrintWriter, FileReader and LineNumberReader as follows.

namespace lines_of_text;

interface

uses
  java.util, java.io;

type
  ConsoleApp = class
  public  
    class method Main(args: array of String);
  end;

implementation

class method ConsoleApp.Main(args: array of String);

var
  MyStrings : array[0 .. 9] of String;
  pw : PrintWriter;
  fr : FileReader;
  lnr : LineNumberReader;

begin
  pw := new PrintWriter('test.txt');
  for i : Integer := 1 to 10 do
    pw.println('Line ' + i.toString); 
  pw.close; 
  fr := new FileReader('test.txt');
  lnr := new LineNumberReader(fr);
  for i : Integer := 0 to 9 do
    MyStrings[i] := lnr.readLine;
  fr.close;   
  for i : Integer := 0 to 9 do
    System.out.println(MyStrings[i]);   
  System.in.read;
end;

end.

(For an alternative method using a FileInputStream, InputStreamReader and LineNumberReader see the conversion of Crossword).

For other tasks, we recommend the use of a RandomAccessFile. This handles the common file types and has writeUTF and readUTF methods to write and read strings. The length of each string accompanies the characters, obviating the need for separators such as end of line markers. You can use the seek method to locate the end of the file in order to append data. Open the file for reading and writing ('rw') to write and for reading only ('r') to read.

namespace random_access;

interface

uses
  java.util, java.io;

type
  ConsoleApp = class    
  public
    class method Main(args: array of String);
  end;

implementation

class method ConsoleApp.Main(args: array of String);
var
  raf : RandomAccessFile;
  line : String := '';
  MyString : String;
  MyChar : Char;
  MyOrd : Integer;
  MyFloat : Float;
  MyInt : Integer;
  MyInt64 : Int64;
  Eoln : String := System.lineSeparator;  
begin
  //Create random access file and write to it 3 line-terminated Strings, 
  //then String, Char, Float, Int and Int64. 
  raf := new RandomAccessFile('test.txt', 'rw');
  raf.writeBytes('We use the writeBytes method to write our strings,' + Eoln); 
  raf.writeBytes('and then the line separator.' + Eoln);
  raf.writeBytes('We can then read each string using the readLine method.' + Eoln);
  raf.writeUTF('This can be any length and does not end with a new line.');
  raf.writeChar(ord('a'));
  raf.writeFloat(3.12156);
  raf.writeInt(123);
  raf.writeLong(10000000); 
  raf.close;
  //Open random access file for reading and read from it
  //3 Strings and output them. 
  raf := new RandomAccessFile('test.txt', 'r');
  for i : Integer := 1 to 3 do
    begin
      line := raf.readLine;
      System.out.println(line);
    end;
  //Read String, Char, Float, Int and Int64.
  MyString := raf.readUTF;
  MyChar := raf.readChar;
  MyFloat := raf.readFloat;
  MyInt := raf.readInt;
  MyInt64 := raf.readLong;
  //Output values of Float, Int and Int64.
  System.out.println('String: ' + MyString);
  System.out.println('Single character read from file: ' + MyChar);
  System.out.println('Float read from file: ' + MyFloat.toString);
  System.out.println('Integer read from file: ' + MyInt.toString);
  System.out.println('Int64 read from file: ' + MyInt64.toString); 
  raf.close;
  System.in.read;
end;

end.

The next demonstration shows how you can write and read variable length records. An object consisting of fields and no methods is a record.

namespace random_access2;

interface

uses
  java.util, java.io;

type
  ConsoleApp = class    
  public
    class method Main(args: array of String);
  end;
  //Variable length record
  AgeRecord = class
  public
    Surname : String;
    Age : Integer;
  end;

implementation

class method ConsoleApp.Main(args: array of String);
var
  AgeRecords, AgeRecs : array[0 .. 2] of AgeRecord; 
  raf : RandomAccessFile;
  i : Integer;
begin
  AgeRecords[0] := new AgeRecord;
  AgeRecords[0].Surname := 'Shah';
  AgeRecords[0].Age := 16;
  AgeRecords[1] := new AgeRecord;
  AgeRecords[1].Surname := 'Ennis';
  AgeRecords[1].Age := 22;
  AgeRecords[2] := new AgeRecord;
  AgeRecords[2].Surname := 'Biggs';
  AgeRecords[2].Age := 55;

  raf := new RandomAccessFile('test.txt', 'rw');
  for i := 0 to 2 do
    begin
      raf.writeUTF(AgeRecords[i].Surname);
      raf.writeInt(AgeRecords[i].Age);
    end;
  raf.close;
  //Open random access file for reading and read 3 records from it.

  raf := new RandomAccessFile('test.txt', 'r');
  for i := 0 to 2 do
    begin
      AgeRecs[i] := new AgeRecord;
      AgeRecs[i].Surname :=raf.readUTF;
      AgeRecs[i].Age := raf.readInt;
    end;
  raf.close;
  for i := 0 to 2 do
    begin
      System.out.print(AgeRecs[i].Surname + ': ') ;
      System.out.println(AgeRecs[i].Age);
    end;
  System.in.read;
end;

end.

Follow the link at the foot of the page to further examples. These cover the writing and reading of arrays of integers and real numbers and appending and editing data.

Equivalent Code of lines_of_text in RemObjects C#

using java.util;
using java.io;

namespace lines_of_text_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      String[] myStrings = new String[10];
      PrintWriter pw;
      FileReader fr;
      LineNumberReader lnr;
      pw = new PrintWriter("test.txt");
      for (int i = 1; i <= 10; i++)
      {
        pw.println("line " + i.toString()); 
      }
      pw.close(); 
      fr = new FileReader("test.txt");
      lnr = new LineNumberReader(fr);
      for (int i = 0; i <= 9 ; i++)
      {
        myStrings[i] = lnr.readLine();
      }
      fr.close();   
      for (int i = 0 ; i <= 9; i++)
      {
        System.@out.println(myStrings[i]); 
      }
      System.@in.read();
    }
  }
}

Equivalent Code of random_access in RemObjects C#

using java.util;
using java.io;

namespace random_access_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      RandomAccessFile raf;
      String line = "";
      String  myString;
      Char myChar;
      Float myFloat;
      Integer myInt;
      Int64 myInt64;
      String Eoln  = System.lineSeparator(); 
      // Create random access file and write to it 3 line-terminated
      // Strings, then String, Char, Float, Int and Int64. 
      raf = new RandomAccessFile("test.txt", "rw");
      raf.writeBytes("We use the writeBytes method to " +  
                     "write our strings," + Eoln); 
      raf.writeBytes("and then the line separator." + Eoln);
      raf.writeBytes("We can then read each string using the " +
                     "readline method." + Eoln);
      raf.writeUTF("This can be any length and does not end " +
                    "with a new line.");
      raf.writeChar('a');
      raf.writeFloat(3.12156);
      raf.writeInt(123);
      raf.writeLong(10000000); 
      raf.close();
      //Open random access file for reading and read from it
      //3 Strings and output them. 
      raf = new RandomAccessFile("test.txt", "r");
      for (Integer i = 1; i <= 3; i++)
      {
        line = raf.readLine();
        System.@out.println(line);
      }
      //Read String, Char, Float, Int and Int64.
      myString = raf.readUTF();
      myChar = raf.readChar();
      myFloat = raf.readFloat();
      myInt = raf.readInt();
      myInt64 = raf.readLong();
      //Output values of Float, Int and Int64.
      System.@out.println("String: " + myString);
      System.@out.println("Single character read from file: " + myChar);
      System.@out.println("Float read from file: " + myFloat.toString());
      System.@out.println("Integer read from file: " + myInt.toString());
      System.@out.println("Int64 read from file: " + myInt64.toString()); 
      raf.close();
      System.@in.read();
    }            
  }
}

Equivalent Code of random_access2 in RemObjects C#

using java.util;
using java.io;

namespace random_access2_cs_java
{  
  class ageRecord 
  {
    public String surname;
    public Integer age;
  }

  static class Program
  {
    private 
      ageRecord[] ageRecords = new ageRecord[3];
      ageRecord[] ageRecs = new ageRecord[3];
    public static void Main(string[] args)
    {
      RandomAccessFile  raf;
      ageRecords[0] = new ageRecord();
      ageRecords[0].surname = "Shah";
      ageRecords[0].age = 16;
      ageRecords[1] = new ageRecord();
      ageRecords[1].surname = "Ennis";
      ageRecords[1].age = 22;
      ageRecords[2] = new ageRecord();
      ageRecords[2].surname = "Biggs";
      ageRecords[2].age = 55;
      raf = new RandomAccessFile("test.txt", "rw");
      for (Integer i = 0; i <= 2; i++)
      {
        raf.writeUTF(ageRecords[i].surname);
        raf.writeInt(ageRecords[i].age);
      }
      raf.close();
      //Open random access file for reading and read 3 records from it.
      raf = new RandomAccessFile("test.txt", "r");
      for (Integer i = 0; i <= 2; i++)
      {
        ageRecs[i] = new ageRecord();
        ageRecs[i].surname = raf.readUTF();
        ageRecs[i].age = raf.readInt();
      }
      raf.close();
      for (Integer i = 0; i <= 2; i++)
      {
        System.@out.print(ageRecs[i].surname + ": ") ;
        System.@out.println(ageRecs[i].age);
      }
      System.@in.read();
    }    
  }
}
Programming - a skill for life!

Input, output and access to a Firebird database in Oxygene for Java console applications