aqFile.OpenTextFile Method

Applies to TestComplete 14.20, last modified on September 11, 2019

Description

The OpenTextFile method locates the specified file and opens it for reading and/or writing 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

Defines the file access type. The following constants and values are possible:

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

One of the following constants that specify the file’s character encoding:

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

OverwriteOrCreate

Indicates what the method will do depending on whether the file to be opened exists or not.

File already exists
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will fail. The method will open the file.
False The method will overwrite the file and will then open it. The method will open the file.
File does not exist
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will fail. The method will fail.
False The method will create a new file at the specified location and will then open it. 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

Once the file is opened only for reading, the aqTextFile.Cursor property of the corresponding object is positioned at the beginning of the file. If the file is opened for writing, or both for reading and writing, the property is positioned at the end of the file. To start working with the file, you should 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
aqFile.OpenBinaryFile Method
OpenTextFile Method

Highlight search results