WriteLine Method

Applies to TestComplete 14.40, last modified on April 22, 2021

Description

Use the WriteLine method to write the specified value to a text file and insert a line break after the written data. The value is written starting from the current position. If the position is not the end of the file, the specified value overwrites the previous data.

Declaration

aqTextFileObj.WriteLine(Value)

aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object
Value [in]    Required    Variant    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Value

Defines a variant-compatible value to be written to a file.

Result Value

True if the value was written successfully, and False otherwise.

Remarks

On success, the cursor position is shifted by the length of the written data.

To use this method, you should open the needed file with the faWrite or faReadWrite access permission. See the description of the OpenTextFile method.

Example

The code below creates a new text file and then writes text to it.

JavaScript, JScript

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for writing
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine("line1");
  myFile.WriteLine("line2");
  myFile.WriteLine("line3");

  // Closes the file
  myFile.Close();
}

Python

def WriteTextToFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Creates a text file at the specified location
  aqFile.Create(sPath)
  # Opens the created file for writing
  myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode)
  # Writes text to the file
  myFile.WriteLine("line1")
  myFile.WriteLine("line2")
  myFile.WriteLine("line3")
  # Closes the file
  myFile.Close()

VBScript

Sub WriteTextToFile
  Dim sPath
  sPath = "D:\Files\MyFile.txt"

  ' Creates a text file at the specified location
  aqFile.Create(sPath)

  ' Opens the created file for writing
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode)

  ' Writes text to the file
  myFile.WriteLine("line1")
  myFile.WriteLine("line2")
  myFile.WriteLine("line3")

  ' Closes the file
  Call myFile.Close()
End Sub

DelphiScript

procedure WriteTextToFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for writing
  myFile := aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine('line1');
  myFile.WriteLine('line2');
  myFile.WriteLine('line3');

  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile["Create"](sPath);

  // Opens the created file for writing
  var myFile = aqFile["OpenTextFile"](sPath, aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile["WriteLine"]("line1");
  myFile["WriteLine"]("line2");
  myFile["WriteLine"]("line3");

  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
Write Method
ReadLine Method
ReadString Method
ReadToSymbol Method
ReadAll Method
WriteBlankLines Method

Highlight search results