Home  |  Delphi .net Home  |  System.Console  |  SetOut Method
  
SetOut  
Method  
Assigns a new Standard Output Stream writer
Console Class
System NameSpace
CF1.  Procedure SetOut ( NewWriter : TextWriter ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Assigns the Out property to NewWriter, allowing a different target for text lines from a Console application.
 
As in the example, this can be used to write to a text file instead.
Notes
Static methods are not methods of an object - they are simply class functions or procedures available at any time.
Microsoft MSDN Links
System
System.Console
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  fileName   : String;

  consoleOut : TextWriter;
  reader     : TextReader;
  writer     : TextWriter;

begin
  // Create a test text file
  fileName := 'C:DelphiBasics.txt';
  if not System.IO.File.Exists(fileName)
  then System.IO.File.CreateText(fileName);

  // Open the file
  writer := StreamWriter.Create(fileName);

  // Save the existing Console Out stream
  consoleOut := Console.Out;

  // Assign the Console Out stream to this file
  Console.SetOut(writer);

  // Write to this file
  Console.WriteLine('Hello');
  Console.WriteLine('cruel');
  Console.WriteLine('World');

  // Close the file
  writer.Close;

  // Restore the standard output to the console
  Console.SetOut(consoleOut);

  // Read from the file
  reader := StreamReader.Create(fileName);

  Console.WriteLine(reader.ReadLine);
  Console.WriteLine(reader.ReadLine);
  Console.WriteLine(reader.ReadLine);

  Console.ReadLine;
end.
   Hello
   cruel
   World
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author