Programming | Advanced 5.5.1 ways to create TextStream objects
There are three common methods for creating or opening a text file and returning the Textstram object, as shown in table 5-13:
Table 5-13 methods and descriptions for creating TextStream objects
Method
Description
CreateTextFile
(Filename,overwrite,unicode)
Creates a new text file on disk with the specified filename filename, and returns a TextStream object corresponding to the file. If the optional overwrite parameter is set to True, a file of the same name with the same path will be overwritten. The default overwrite is false. If the optional Unicode parameter is set to False, the contents of the file are stored in Unicode format. The default Unicode is False
OpenTextFile
(Filename,iomode,create,format)
Open or create (if not present) a file named filename, and return the TextStream object corresponding to the file. The filename parameter can contain an absolute or relative path. The IOMode parameter describes the type of access required. Allowable values are ForReading (1) (default), ForWriting (2), ForAppending (8). When you write to or append to a file that does not exist, a new file is created if the Create argument is set to true. The default create is false. The format parameter describes the data formatting when you read or write a file. The allowable value is Tristatefalse (0) (default), the description is in ASCII data format, TristateTrue (-1) description is in Unicode data format, Tristateusedefault (-2) indicates that the data uses the system default format
OpenAsTextStream
(Iomode,format)
Opens a specified file and returns a TextStream object that can be used to read, write, or append to the file. The IOMode parameter illustrates the type of access that is required. Allowable values are ForReading (1) (default), ForWriting (2), ForAppending (8). The format parameter describes the data formats for reading and writing files. The allowable value is Tristatefalse (0) (default), the description is in ASCII data format, TristateTrue (-1) description is in Unicode data format, Tristateusedefault (-2) indicates using system default format
The methods listed above differ in the implementations in FileSystemObject, folder, and file objects. As shown in table 5-14:
Table 5-Methods contained in 143 objects
Method
FileSystemObject objects
Folder Object
File Object
CreateTextFile
Yes
Yes
Yes
OpenTextFile
Yes
No
No
OpenAsTextStream
No
No
Yes
Therefore, you can use these methods to create a new text file, or to open an existing file. You can get a TextStream object corresponding to the file, and you can manipulate the file using the properties and methods of the TextStream object.
1. Create a new text file
You can create a new text file by using the CreateTextFile method, or overwrite a file that already exists. The returned TextStream object can be used to read and write files.
First create a FileSystemObject object to create the TextStream object. The following example uses VBScript to create a "normal" (i.e., non-Unicode) file named MyFile.txt, overwriting a file with the same name that already exists:
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set Objtstream = objFSO.CreateTextFile ("C:\TextFiles\MyFile.txt", True, False)
The same can be accomplished with JScript:
var objFSO = Server.CreateObject (' Scripting.FileSystemObject ');
var objtstream = objFSO.CreateTextFile (' C:\TextFiles\MyFile.txt ', true, false);
Once you create the file, you can use Objtstream, which is a reference to a TextStream object, to manipulate the file.
2. Open a text file that already exists
The OpenTextFile method is used to open an existing text file. It returns a TextStream object that can be used to read or append data to a file.
Again, you first create a FileSystemObject object and then use it to create a TextStream object. The following VBScript program example opens a file named MyFile.txt, ready to read its contents:
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set Objtstream = objFSO.OpenTextFile ("C:\TextFiles\MyFile.txt", ForReading)
with JScript:
var objFSO = Server.CreateObject (' Scripting.FileSystemObject ');
var objtstream = objFSO.OpenTextFile (' C:\TextFiles\MyFile.txt ', ForReading);
To write a file or to create a file that does not exist, you can use the following code:
' In VBScript:
Set Objtstream = objFSO.OpenTextFile ("C:\TextFiles\MyFile.txt", ForWriting, True)
In JScript:
var objtstream = objFSO.OpenTextFile (' C:\TextFiles\MyFile.txt ', ForWriting, true);
If you want to open an existing Unicode file and you are ready to append data to it, but do not create a file that does not exist, you can use:
' In VBScript:
Set Objtstream = objFSO.OpenTextFile ("C:\TextFiles\MyFile.txt", ForReading, _
False, TristateTrue)
In JScript:
var objtstream = objFSO.OpenTextFile (' C:\TextFiles\MyFile.txt ', ForReading, _
Fasle, TristateTrue);
3. Open a File object as a TextStream object
The OpenAsTextStream method of the file object can be used to open the file corresponding to the object, and a TextStream object that can read, write, and append the file is returned. So, given a file object (in this case not a FileSystemObject object)--objfileobject, it can be opened as a "normal" (non-Unicode) TextStream object to append the contents of the file:
' In VBScript:
Set Objtstream = Objfileobject.openastextstream (ForAppending, False)
In JScript:
var objtstream = Objfileobject.opentextfile (ForAppending, false);
Note that you do not need a filename to use this method because the execution of the program is done by referencing the file object, and there is no create argument because the file must already exist, and if you want to start with a new empty file, you can use:
' In VBScript:
Set Objtstream = Objfileobject.openastextstream (ForWriting)
In JScript:
var objtstream = Objfileobject.op