Home  |  Delphi .net Home  |  System.IO.File  |  OpenWrite Method
OpenWrite  
Method  
Opens the specified file for writing to only
File Class
System.IO NameSpace
CF1.  Function OpenWrite ( PathString : String; ) : FileStream; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns a FileStream object that allows the file in the specified PathString to be written to.
References
FileStream
Microsoft MSDN Links
System.IO
System.IO.File
 
 
Open a file for writing and then write to it and read from it
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Path   : String;
  Stream : System.IO.FileStream;
  Reader : StreamReader;

begin
  // Open the file for writing
  Path := 'C:\DelphiBasics.txt';
  Stream := System.IO.File.OpenWrite(path);

  // 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
  Reader := System.IO.File.OpenText(Path);

  // Display the contents
  Console.WriteLine(Reader.ReadToEnd);

  // Close the stream
  Stream.Close;

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