Yesterday (24th June 2008), one of SQAForums member had an query for changing the file format of any file from ANSI to UTF-8. Below is the link to the same query
Though i replied to the poster to do it using DotNetFactory but there was something that was still making me feel uneasy. With in few mins i knew what was making me feel uneasy, it was NOT knowing how to actually do it. That was it and i opened my Visual Studio 2005 IDE. Started exploring various things in the object browser and finally with some R&D effort came up with very simple code to write files in desired encoding.
This article will explore how to write files in various formats UTF-7, UTF-8, UTF-32, ASCII, Unicode and Big Endian Unicode. All this code would be for QTP and we would be using DotNetFactory for creating the .NET Class objects
Step 1:
First stpe is to create an object of class System.Text.Encoding to get access to various encoding formats
'Create the .NET object for getting encoding formats
Set oEncoder = DotNetFactory("System.Text.Encoding")
'Get various encoding formats
With oEncoder
Set UTF7 = .UTF7
Set UTF8 = .UTF8
Set UTF32 = .UTF32
Set ASCII = .ASCII
Set UNICODE = .UNICODE
Set BigEndianUNICODE = .BigEndianUnicode
End With
Step 2:
Next is to create a file in desired format using System.IO.StreamWriter class of .NET. This can be done as illustrated in code below
'File to write to
sFileName = "C:\Test.txt"
'We don't want to append if file already exists. Just overwrite existing content
bAppendToExistingFile = False
'Encoding to use
Set oEncodingToUse = UTF32
'Create a file stream in required encoding format
Set oFile = DotNetFactory("System.IO.StreamWriter", , sFileName, bAppendToExistingFile, oEncodingToUse)
'Write something on the file
oFile.Write "Tarun "
oFile.WriteLine "Lalwani"
'Close the file
oFile.Close
'cleanup objects
oFile.dispose
Set oFile = Nothing
Set oEncoder = Nothing
Files that we create using different format might look same in a notepad, but they are not actually same in the way they are stored. To observe the difference we will use the Edit command of MS-DOS.
File encoded in UTF-32 format
Similarly we can also read files using System.IO.StreamReader class of .NET as shown in below code
'File to read from
sFileName = "C:\Test.txt"
'Encoding to use
Set oEncodingToUse = UTF32
'Create a file stream in required encoding format
Set oFile = DotNetFactory("System.IO.StreamReader", , sFileName, oEncodingToUse)
'Loop until we reach end of stream
While Not oFile.EndOfStream
'Read and print line by line
Print oFile.ReadLine()
Wend
'Close the file
oFile.Close
'cleanup objects
oFile.dispose
Set oFile = Nothing
Set oEncoder = Nothing
Above code would produce the below output in the QTP print window
File read using UTF-32 encoding
DotNetFactory adds a lot of power to QTP, but to see the real beauty you need to unleash it yourself. This article is a mere example