More File I/O Examples

These examples reinforce file handling by providing Oxygene versions of programs in the Input to File and Output from File sections of our Pascal I/O tutorial. Note the use of square brackets for initialising the contents of an array.

Writing and Reading an Array of Integers

namespace save_integers;

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);
const
  AverageLondonRainfall : array [0 .. 11] of Integer =
                          [61, 36, 50, 42, 45, 46, 46, 44, 43, 73, 45, 59];
var
  Count : Integer;
  RainFile : RandomAccessFile;
begin
  RainFile := new RandomAccessFile('rain.dat', 'rw');
  System.out.println('Writing Integers to rain.dat');
  for Count := 0 to 11 do
    RainFile.write(AverageLondonRainfall[Count]);
  RainFile.close;
  System.in.read;
end;

end.

Note that before running read_integers you must copy the data file from save_integers to bin\Debug or bin\Release as appropriate.

namespace read_integers;

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
  AverageLondonRainfall : array[0 .. 11] of Integer;
  Count : Integer;
  RainFile : RandomAccessFile;
begin
  RainFile := new RandomAccessFile( 'rain.dat', 'r');
  System.out.println('Reading integers from rain.dat');
  for Count := 0 to 11 do
    AverageLondonRainfall[Count]:= RainFile.read;
  RainFile.close;
  System.out.print('Average London rainfall (Jan to Dec, mm): ');
  for Count := 0 to 11 do
    System.out.print(AverageLondonRainfall[Count].toString + ' ');
  System.out.println;
  System.in.read;
end;

end.

Writing and Reading an Array of Reals

namespace save_reals;

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

const
  TemperatureReadings : array [0 .. 5] of Single =
                        [70.0, 64.2, 59.9, 57.1, 55.0, 53.4];
var
  Count : Integer;
  TFile : RandomAccessFile;
begin
  TFile := new RandomAccessFile('Temps.txt', 'rw');
  System.out.println('Writing Singles to Temps.txt');
  for Count := 0 to 5 do
     TFile.writeFloat(TemperatureReadings[Count]);
  TFile.close;
  System.in.read;
end;

end.

Note that before running read_reals you must copy the data file from save_reals to bin\Debug or bin\Release as appropriate.

namespace read_reals;

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
  TemperatureReadings : array [0 .. 5] of Single;
  Count : Integer;
  TFile : RandomAccessFile;
begin
  TFile := new RandomAccessFile('Temps.txt', 'r');
  System.out.println('Reading Singles from Temps.txt');
  for Count := 0 to 5 do
    TemperatureReadings[Count] := TFile.readFloat;
  TFile.close;
  System.out.print('Temperatures: ');
  for Count := 0 to 5 do
    System.out.printf('%.1f ', TemperatureReadings[Count]);
  System.out.println;
  System.in.read;
  end;

end.

Appending Data

namespace append;

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
  Count : Integer;
  TFile : RandomAccessFile;
  NewTemp : Single := 99.9;
begin
  TFile := new RandomAccessFile('Temps.txt', 'rw');
  TFile.seek(TFile.length);
  System.out.println('Appending Single to Temps.txt');
  TFile.writeFloat(99.9);
  TFile.close;
  System.in.read;
end;

end.

Editing Data

namespace edit_integer;

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
  MonthNo, Rainfall : Integer;
  RainFile : RandomAccessFile;
  InputSBytes : array[0 .. 5] of SByte;
  strMonthNo, strRainfall : String;
begin
  RainFile := new RandomAccessFile('rain.dat', 'rw');
  writeln('For which month number would you like to enter new value? ');
  System.in.read(InputSBytes);
  strMonthNo := new String(InputSBytes);
  MonthNo := Integer.valueOf(strMonthNo.trim);
  writeln('Please enter the new average rainfall. ');
	InputSBytes := [32, 32, 32, 32, 32, 32];
  System.in.read(InputSBytes);
  strRainfall := new String(InputSBytes);
  Rainfall := Integer.valueOf(strRainfall.trim);
  Rainfile.seek(MonthNo - 1); //Data item numbering starts at 0
  System.out.println('Entering new value');
  RainFile.write(Rainfall);
  RainFile.close;
  System.in.read;
end;

end.
Programming - a skill for life!

File Input & Output in Oxygene for Java