Home  |  Delphi .net Home  |  System.IO.FileInfo  |  OpenRead Method
OpenRead  
Method  
Opens the current file for reading only
FileInfo Class
System.IO NameSpace
CF1.  Function OpenRead ( ) : FileStream;
CF : Methods with this mark are Compact Framework Compatible
Description
A FileStream object is returned allowing read only access to the current file.
References
FileStream
Microsoft MSDN Links
System.IO
System.IO.FileInfo
 
 
Open a file for writing and then write to it and read from it
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  FileInfo : System.IO.FileInfo;
  Stream   : System.IO.FileStream;
  MyByte   : Integer;

begin
  // Create a FileInfo object for a text file
  FileInfo := System.IO.FileInfo.Create('C:\DelphiBasics.txt');

  // Open the file for writing
  Stream := FileInfo.OpenWrite;

  // Write to the file
  Stream.WriteByte(65);  // Chr(65) = 'A'
  Stream.WriteByte(66);  // Chr(66) = 'B'
  Stream.WriteByte(67);  // Chr(67) = 'C'

  // Close the file
  Stream.Close;

  // Re-open for reading
  Stream := FileInfo.OpenRead;

  // Display the contents
  MyByte := Stream.ReadByte;
  while MyByte >= 0 do
  begin
    Console.Write(Char(MyByte));
    MyByte := Stream.ReadByte;
  end;

  // Close the stream
  Stream.Close;

  Console.Readline;
end.
Show full unit code
  ABC
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author