5.5.1 Creating a TextStream object There are three common methods to create or open a text file and return the Textstram object.
CreateTextFile (FileName, OverWrite, Unicode) Creates a new text file with the specified file name filename on the disk and returns a TextStream object corresponding to the file. If the optional OverWrite parameter is set to True, the same name file with the same path will be overridden. The default OverWrite is False. If the optional Unicode parameter is set to false, the content of the file will be stored as a Unicode format. The default Unicode is false OpenTextFile (filename, omode, create, format) open or create a file called FileName and returns the TextStream object corresponding to the file. The FileName parameter can contain absolute or relative paths. The Iomode parameter illustrates the required access type. The allowable value is Forreading (1) (default), ForWriting (2), FORAPPPpending (8).
When writing or appending to a non-existing file, if the CREATE parameter is set to True, a new file will be created. The default CREATE is False. The Format parameter illustrates the data format when reading or writing files. The allowable value is Tristatefalse (0) (default), illustrating the ASCII data format; TristateTrue (-1) Describes the Unicode data format; TristateUsesedefault (-2) Description Data Using System Default Format
OpenastextStream (Iomode, Format) opens a specified file and returns a TextStream object, which can be used to read, write, or append the file. The Iomode parameter illustrates the required access type. The allowable value is Forreading (1) (default), ForWriting (2), FORAPPPpending (8). The Format parameter illustrates the data format of the read and write file. The allowable value is Tristatefalse (0) (default), which illustrates the ASCII data format; TristateTrue (-1) Describes the Unicode data format; TristateuseDefault (-2) Description Use the method listed above using the system in FileSystemObject, Folder Unlike the implementation in the File object. As shown in Table 5-14: Table 5-14 Methods of Methods included in Table 5-14 FileSystemObject Object Folder Object File Object CreateTextFile There is no OpenASTextStream no, there is no, you can use these methods to create a new text file Or open an existing file. You can get a TextStream object with the file, you can use the properties and methods of the TextStream object to operate files. 1. Creating a new text file You can create a new text file with the CreateTextFile method or override an existing file. The returned TextStream object can be used to read and write files. First create a FileSystemObject object to create a TextStream object. The following example is to create a "normal" (ie unicode) named myfile.txt with VBScript, and override the existing same name file: set objfso = server.createObject ("scripting.filesystemObject") set objtstream = Objfso .CreateTextFile ( "C: /TextFiles/MyFile.txt", True, False) which likewise be implemented JScript: var objFSO = Server.CreateObject ( 'Scripting.FileSystemObject'); var objTStream = objFSO.CreateTextFile ( 'C: / TextFiles /Myfile.txt ', true, false; Once this file is created, ObjtStream (it is a reference to a TextStream object). 2. Open the existing text file OpenTextFile method is used to open an existing text file. It returns a TextStream object that can be read or added to the file with this object. Again, first create a FileSystemObject object and create a TextStream object with it.
The following VBScript program example opens a file named myfile.txt, ready to read its content: 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); In order to write a file or create a No files, 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, prepare to add data, 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. As a TextStream object opens a File object to open the file with the object's OpenASTextStream method to open the file corresponding to the object, and return a TextStream object that can read, write, and append the file.
So, given a File object (this case is not a FileSystemObject object) - ObjfileObject, you can open it as a "normal" TextStream object for additional file content: 'in vbscript: set objtstream = ObjfileObject .OpenastextStream (FORAPPENDING, FALSE) // IN jscript: var objtstream = objfileObject.opENTEXTFILE (ForaPpending, false); Note Because the file must already exist, if you want to start from a new empty file, you can use: 'in vbscript: set objtstream = objfileObject.openastextStream (forWriting) // in jscript: var objtstream = objfileObject.OpenTextFile (forwriting); if want to read the file: 'In VBScript: Set objTStream = objFileObject.OpenAsTextStream (ForReading) // In JScript: var objTStream = objFileObject.OpenTextFile (ForReading); 5.5.2 TextStream object members table 5-15 and table 5-16 Summary It is a list of all properties and methods for the TextStream object. The details of each important member will be briefly introduced. 1. The properties of the TextStream object The properties of the TextStream provide information about the current location of the file pointer within the file, as shown in Table 5-15. Note that all attributes are read-only. Table 5-15 Attributes and Description Properties of the TEXTSTREAM object Description AtendOfline If the file position pointer returns true atendofstream if the file position pointer returns true column from 1 Start Return to the list of current characters in the file Line From 1 Start Returns the line number of the current line of the current line and the ATENDOFSTREAM properties are only available to files that are opened by the iOmode parameter for Forreading, otherwise it will be wrong.
2. Table 5-16 shows the method of the TextStream object. ) As a single string reads the entire file readline () as a string reads a line from the file (until the Auto-Yarn and Punction) Skip (Numchars) When reading from the file, Ignore NumChars characters Skipline () when When reading the next line WRITE (String) Writing String String WriteLine (String) Write String String String WriteLine (optional) and wrap WriteBlankLines (n) Write n changefill 3. Write a text file Once you use createTextFile, OpenTextFile or OpenASTextStream method, you create a textStream object that corresponds to a file, you can write files and close files with the following VBScript program: 'In vbscript: objtstream.writeline "At Last I can create files with VBScript "objTStream.WriteLine objTStream.WriteLine" Here are three blank lines:!. "objTStream.WriteBlankLines 3 objTStream.Write" ... and this is "objTStream.WriteLine" the last line "objTStream.Close or a JScript: // In JScript: objTStream.WriteLine ( 'At last I can create files with JScript!'); objTStream.WriteLine (); objTStream.WriteLine ( 'Here are three blank lines:'); objTStream.WriteBlankLines (3 ); ObjtStream.write ('... and this is'); ObjtStream.writeline ('the last line.'); objtstream.close (); 4. Read the text file Once you use createTextFile, OpenTextFile or OpenASTextStream method, create a TextStream object corresponding to a file, you can read files and close files with the following VBScript program: 'In Vbscript:' Read One Line At a Time UnTil the end of the file is reached Do While Not objTStream.AtEndOfStream 'get the line number intLineNum = objTStream.Line' format it as a 4-character string with leading zeros strLineNum = Right ( "000" & CStr (intLineNum), 4) 'get the text of the line from the file strlinetext =
objTStream.ReadLine Response.Write strLineNum & ":" & strLineText & "" Loop objTStream.Close or with JScript: // In JScript: // read one line at a time until the end of the file is reached while (objTStream!. AtEndOfStream) {// get the line number intLineNum = objTStream.Line; // format and convert to a string strLineNum = '000' intLineNum.toString (); strLineNum = substr (strLineNum, strLineNum.length - 4, 4) / / get the text of the line from the file strlinetext = objtstream.readline (); response.write (Strlinenum ');} objtstream.close (); 5.5.3 TextStream object Example for use The TextStream object operates several ways of the abrasive file, this book provides a VBScript sample page that uses a lot of the above code. From the example of the CHAPTER05 main menu page, select the link "Working with the textStream Object" Open the show_textstream.asp page. This page shows the text content of the file stored on the disk name myFile.txt. The text content displayed in the control allows editing and there are three buttons below. The role of the three buttons is the first text of the content update (ie, replacing) of the
'Create An Instance of A FileSytemObject ObjectSet Objfso = Server.createObject ("Scripting.FileSystemObject") ... Like most other examples of this section in the book, this page contains a