WriteBytes Method

Applies to TestComplete 15.70, last modified on December 17, 2024

Description

Use the WriteBytes method to write an array of bytes to a file. You can get an array of bytes by using the aqBinaryFile.ReadBytes or aqHttpRequest.Send method.

Declaration

aqBinaryFileObj.WriteBytes(Data)

aqBinaryFileObj An expression, variable or parameter that specifies a reference to an aqBinaryFile object
Data [in]    Required    A aqBytes object    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Data

Specifies a sequence of bytes to be written to a file.

Result Value

True if the value has been 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 OpenBinaryFile method.

  • The method can take an aqHttpResponse object as a parameter. You can use it to write data returned from a web server to a file. However, to save an HTTP response to a file, we recommend that you use the aqHttpResponse.SaveToFile method.

Example

The code below demonstrates how you can use the WriteBytes method to write a sequence of bytes to a file:

JavaScript, JScript

function Bytes()
{
  Path = "C:\\MyFiles\FileName.txt";
  
  // Reads the content of the specified file
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead);
  
  // Reads bytes from the file
  s = myFile.ReadBytes();
  myFile.Close();
  
  // Writes the received array to the file
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite, true);
  result = myFile.WriteBytes(s);
  
  myFile.Close();
}

Python

def Bytes():
  Path = "C:\\MyFiles\FileName.txt"
  
  # Reads the content of the specified file
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  
  # Reads bytes from the file
  s = myFile.ReadBytes()
  myFile.Close()
  
  # Writes the received array to the file
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite, true)
  result = myFile.WriteBytes(s)
  
  myFile.Close()

VBScript

Sub Bytes
Dim Path, myFile, s, result

  Set Path = "C:\\MyFiles\FileName.txt"
  
  ' Reads the content of the specified file
  Set myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  
  Set s = myFile.ReadBytes()
  
  Call myFile.Close()
  
  ' Writes the received array to the file
  Set myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite, true)
  Set result = myFile.WriteBytes(s)
  
  Call myFile.Close()
End Sub

DelphiScript

procedure Bytes;
var Path, myFile, s, result;
begin
  Path := 'C:\\MyFiles\FileName.txt';
  
  // Reads the content of the specified file
  myFile := aqFile.OpenBinaryFile(Path, aqFile.faRead);
  
  // Reads bytes from the file
  s := myFile.ReadBytes();
  myFile.Close();
  
  // Writes the received array to the file
   myFile := aqFile.OpenBinaryFile(Path, aqFile.faWrite, true);
   result := myFile.WriteBytes(s);
   
   myFile.Close();
end;

C++Script, C#Script

function Bytes()
{
  Path = "C:\\MyFiles\FileName.txt";
  
  // Reads the content of the specified file
  myFile = aqFile["OpenBinaryFile"](Path, aqFile.faRead);
  
  // Reads bytes from the file
  s = myFile["ReadBytes"]();
  myFile["Close"]();
  
  // Writes the received array to the file
  myFile = aqFile["OpenBinaryFile"](Path, aqFile.faWrite, true);
  result = myFile["WriteBytes"](s);
  
  myFile["Close"]();
}

See Also

Working With Files From Scripts
ReadBytes Method
WriteByte Method
aqHttpResponse Object
aqBytes Object

Highlight search results