Applies to TestComplete 14.50, last modified on August 05, 2020

Description

The OpenTextFile method locates the specified file and opens it for reading or writing or both in text mode using the given encoding type.

Declaration

aqFile.OpenTextFile(PathFileAccessTextCodingTypeOverwriteOrCreate)

Path [in]    Required    String    
FileAccess [in]    Required    Integer    
TextCodingType [in]    Required    Integer    
OverwriteOrCreate [in]    Optional    Boolean Default value: False   
Result An aqTextFile object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Path

Specifies the fully-qualified path to the desired file.

FileAccess

An integer value that defines the file access type. You can specify one of the possible integer values directly, or you can use the appropriate constant:

Constant Value Description
aqFile.faWrite 10 Open the file for writing.
aqFile.faRead 11 Open the file for reading.
aqFile.faReadWrite 12 Open the file both for reading and writing.

TextCodingType

An integer value that defines a file’s character encoding. You can specify one of the possible integer values directly, or you can use the appropriate constant:

Constant Value Description
aqFile.ctANSI 20 ANSI
aqFile.ctUnicode 21 Unicode (UTF-16)
aqFile.ctUTF8 22 UTF8

OverwriteOrCreate

A Boolean value that specifies method behavior depending on whether the file exists or not and on the specified FileAccess parameter:

File already exists
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will not be able to overwrite the file opened for reading only and will fail. The method will open the file for writing or for reading and writing and overwrite the file contents.
False The method will open the file for reading. The method will open the file for writing or for reading and writing and leave the file contents.
File does not exist
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will not be able to create the file and will fail. The method will create the file and will open it for writing or for reading and writing.
False The method will fail. The method will fail.

Result Value

An aqTextFile object that corresponds to the specified file. To perform operations over the opened file, use the methods and properties of the returned object.

Remarks

If the file is opened for reading only, the aqTextFile.Cursor property is pointing to the beginning of the file.

If the file is opened for writing or for both reading and writing, the aqTextFile.Cursor property is pointing to the end of the file. You can set this property to point to the beginning of the file (assign to zero) or to any desired position within the file.

Example

The sample routines below demonstrate how you can read data from and write it to text files using the OpenTextFile method.

The following script routine 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
  Call aqFile.Create(sPath)

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

  ' Writes text to the file
  Call myFile.WriteLine("line1")
  Call myFile.WriteLine("line2")
  Call 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"]();
}

The following script routine demonstrates how you can read data from a text file:

JavaScript, JScript

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

  // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
  Log.Message("File by lines:");

  // Reads text lines from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile.ReadLine();
    Log.Message(s);
  }

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

Python

def ReadTextFromFile():
  sPath = "D:\\Files\\MyFile.txt"
  
  # Opens the specified file for reading
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")
  
  # Reads text lines from the file and posts them to the test log 
  while not myFile.IsEndOfFile():
    s = myFile.ReadLine()
    Log.Message(s)
  
  # Closes the file
  myFile.Close()

VBScript

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

  ' Opens the specified file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")

  ' Reads text lines from the file and posts them to the test log
  While Not myFile.IsEndOfFile()
    s = myFile.ReadLine()
    Log.Message(s)
  WEnd

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

DelphiScript

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

  // Opens the specified file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
  Log.Message('File by lines:');

  // Reads text lines from the file and posts them to the test log
  while not myFile.IsEndOfFile() do
    begin
      s := myFile.ReadLine();
      Log.Message(s);
    end;

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

C++Script, C#Script

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

  // Opens the specified file for reading
  var myFile = aqFile["OpenTextFile"](sPath, aqFile.faRead, aqFile.ctUnicode);
  Log["Message"]("File by lines:");

  // Reads text lines from the file and posts them to the test log
  while(! myFile["IsEndOfFile"]())
  {
    s = myFile["ReadLine"]();
    Log["Message"](s);
  }

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

See Also

Working With Files From Scripts
aqTextFile Object
OpenBinaryFile Method
OpenTextFile Method

Highlight search results