Tutorials to .com

Tutorials to .com » Asp » Base » Advanced Programming ASP 3.0 (22)

Advanced Programming ASP 3.0 (22)

Print View , by: iSee ,Total views: 10 ,Word Count: 3214 ,Date: Sat, 25 Apr 2009 Time: 11:18 PM

5.5.1 Object creation TextStream
There are three common methods used to create or open a text file and return to TextStram object, such as shown in Table 5-13:
Table 5-13 to create the object and TextStream Description
Methods
Description

CreateTextFile
(filename, overwrite, unicode)
In the disk file name with the specified filename to create a new text file and returns a document with the corresponding object TextStream. If the optional overwrite parameter is set to True, will cover the path of the same name have the same document. Overwrite the default is False. If unicode optional parameters set to False, the contents of the file will be stored as Unicode format. Unicode default is False

OpenTextFile
(filename, iomode, create, format)
Open or create (if it does not exist) the file named filename, and return the document with the corresponding object TextStream. filename parameter can contain absolute or relative path. iomode parameters indicate a need for the access type. Allowed value is ForReading (1) (default), ForWriting (2), ForAppending (8). Write or additional to a file does not exist, if the create parameter set to True, will create a new document. Create the default is False. describes the format parameter read or write data file format. Allowed value is TristateFalse (0) (default), indicating that data using ASCII format; TristateTrue (-1) that use Unicode data format; TristateUseDefault (-2) indicate the data format using the system default

OpenAsTextStream
(iomode, format)
Open a specified file and returns a TextStream object, the document can be used to read, write, or additional. parameters iomode the need for the access type. Allowed value is ForReading (1) (default), ForWriting (2), ForAppending (8). Format parameter describes the data format to read and write documents. Allowed value is TristateFalse (0) (default), indicating that data using ASCII format; TristateTrue (-1) that use Unicode data format; TristateUseDefault (-2) use the system default format

Methods listed above in the FileSystemObject, Folder and File object to achieve different. Table 5-14 as follows:
Table 5-14 contains three object methods
Methods
FileSystemObject Object
Folder object
File Object

CreateTextFile
Have
Have
Have

OpenTextFile
Have
Without
Without

OpenAsTextStream
Without
Without
Have

Therefore, these methods can be used to create a new text file, or open an existing document. The document can be obtained with a corresponding TextStream object, you can use TextStream object properties and methods of operation of a document.
1. Create a new text file
CreateTextFile method can be used to create a new text file, or overwrite an existing file. TextStream returned object can be used to read and write files.
First of all, to create a FileSystemObject object, the object used to create TextStream. The following example is a vbscript to create a "normal" (that is, non-Unicode) called MyFile.txt documents, and coverage of the same name already exists documents:
Set objFSO = Server.CreateObject ( "Scripting.FileSystemObject")
Set objTStream = objFSO.CreateTextFile ( "C: \ TextFiles \ MyFile.txt", True, False)
jscript can be used to achieve the same:
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 (it is the object of a reference TextStream) operate on the document.
2. Open the text file already exists
OpenTextFile method has been applied to open an existing text file. TextStream it returns an object, this object can be used to read or additional data file.
Similarly, first create a FileSystemObject object and then create a TextStream its target. The following example VBScript program to open a document MyFile.txt prepared to read out its contents:
Set objFSO = Server.CreateObject ( "Scripting.FileSystemObject")
Set objTStream = objFSO.OpenTextFile ( "C: \ TextFiles \ MyFile.txt", ForReading)
Using 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 document 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 files, ready to additional data, but does not create the file does not exist, can be used:
'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. TextStream object as a File object to open a
File objects can be used to open OpenAsTextStream Method object with the appropriate file, and return the document a read, write, and additional objects TextStream. Therefore, given a File object (this case is not FileSystemObject Object) - objFileObject, can be used as a "normal" (non-Unicode) TextStream object to open it for additional content of the document:
'In VBScript:
Set objTStream = objFileObject.OpenAsTextStream (ForAppending, False)

/ / In JScript:
var objTStream = objFileObject.OpenTextFile (ForAppending, false);
Note that using this method do not need to file name, since the implementation of the procedures carried out by reference File object and not create the parameters, because the file must already exist, if you want a new space from the documents can be used:
'In VBScript:
Set objTStream = objFileObject.OpenAsTextStream (ForWriting)

/ / In JScript:
var objTStream = objFileObject.OpenTextFile (ForWriting);
If you want to read the document:
'In VBScript:
Set objTStream = objFileObject.OpenAsTextStream (ForReading)

/ / In JScript:
var objTStream = objFileObject.OpenTextFile (ForReading);

Summary 5.5.2 TextStream members object
Table 5-15 and Table 5-16 are all TextStream object a list of properties and methods. The following brief introduction will be a member of various important details.
1. TextStream object attributes
TextStream attributes to provide the relevant documents within the file pointer of current location information, such as shown in Table 5-15. Note that all of the attribute is read-only.
Table 5-15 TextStream and explain the properties of the object
Properties
Description

AtEndOfLine
If the file position pointer in the document is returned at the end of his True

AtEndOfStream
If the file position pointer at the end of the document is returned True

Column
From the beginning of a return to the document set out the current number of characters

Line
From 1 to return to document the beginning of the current line number line

AtEndOfLine and AtEndOfStream attribute to iomode parameters only way ForReading open file available, it will be wrong.
2. TextStream object
TextStream object as shown in Table 5-16:
Table 5-16 TextStream object and that
Methods
Description

Close ()
The closure of an open file

Read (numchars)
Read out from the document character numchars

ReadAll ()
Read out the string as a single document as a whole

ReadLine ()
As a string read from the document line (until the carriage return and line feed)

Skip (numchars)
When read from the file ignoring numchars characters

SkipLine ()
When read from the file when the next line ignored

Write (string)
Write string to file string

WriteLine (string)
Write string to file string (optional), and line breaks

WriteBlankLines (n)
Write to the file of newline n

3. Write a text file
By using CreateTextFile, OpenTextFile or ForWriting or OpenAsTextStream methods and ForAppending parameters, create a file corresponds to a TextStream object, can be used to write the following VBScript program documents and close the document:
'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 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. Reading text files
By using CreateTextFile, OpenTextFile or OpenAsTextStream methods and ForReading parameters, create a file corresponds to a TextStream object, VBScript can use the following procedure documents and close the document read:
'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 & "<BR>"
Loop
objTStream.Close
Or 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 + ':' + strLineText + '<BR>');
)
objTStream.Close ();

5.5.3 TextStream object, for example
In order to understand the operation of the use of indelible TextStream document object in several ways, this book provides a sample VBScript pages, the page to use a large number of the above-mentioned code. Chapter05 from the main menu sample page, select the link "Working With the TextStream Object" to open the page show_textstream.asp.
This page shows the disk is stored in a document called MyFile.txt text. <TEXTAREA> Controls in the display in the text allows for editing, and there are three buttons below. The role of three buttons are used to update the contents of control <TEXTAREA> (ie replace) the original text, in the back of the contents of the documents have to add text, or rewrite the contents of the initial default document, Figure 5-15 follows:

Figure 5-15 TextStream object sample page
1. Already exists to read the contents of a text file
Every time loading the page, will open the text file and read the contents into <TEXTAREA> control. Note how we use Server.MapPath method MyFile.txt document's absolute physical path of the document with the sample page in the same directory. The following procedure to create FileSystemObject example:
<%
strTextFile = Server.MapPath ( "MyFile.txt")

'create an instance of a FileSytemObject object
Set objFSO = Server.CreateObject ( "Scripting.FileSystemObject")
...
With the book in this section, like most other examples, the page contains a paragraph <FORM> in order to preserve control of the HTML page. ACTION is the current page, so the contents of the form to send back to the same page.
Each time the page is loading, <TEXTAREA> control with the current contents of a text file filled with:
...
<FORM ACTION = "<% = Request.ServerVariables (" SCRIPT_NAME ")%>" METHOD = "POST">

The contents of the disk file <B> <% = strTextFile%> </ B> are: <P>
<TEXTAREA NAME="txtContent" ROWS="10" COLS="50" WRAP="PHYSICAL">
<%
'open the text file as a TextStream object
Set objTStream = objFSO.OpenTextFile (strTextFile, ForReading)
'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 and convert to a string
strLineNum = Right ( "00" & CStr (intLineNum), 3)
'get the text of the line from the file
strLineText = objTStream.ReadLine
Response.Write strLineNum & ":" & strLineText & vbCrLf
Loop
objTStream.Close
%>
</ TEXTAREA> <P>
By the above procedure could know how to open the text file to read, traverse the entire document to read each line (as a string rather than read out the document as a whole). This is because you want to add their own line number, line number does not belong to the text of the document. From the document read out each line (before time), search and Line formatting attributes and create a three-digit line number. And then the line number and placement of text lines within the control <TEXTAREA> page.
2. Update the contents of a text file
When you click the Update button page (General <TEXTAREA> in the edited text control in the future), will be in control of the content of <TEXTAREA> re-written to the text document. To this end, the page number corresponding asp code to create HTML controls in the previous test Request.Form collection to view the click of a button which (if any), and then reload the page.
If you click the Update button, the contents of the collection of <TEXTAREA> control as a string, separated from the string into an array of independent choice of text, and open the text file ready to rewrite its contents, and then traverse the array you just created, by line Cycle No. write the contents of the bank:
...
'look for a command sent from the FORM section buttons
If Len (Request.Form ( "cmdUpdate")) Then
'Get contents of TEXTAREA control
strNewText = Request.Form ( "txtContent")
'Split it into an array of lines at each carriage return
arrLines = Split (strNewText, vbCrLf)
'Open the text file for writing, which replaces all existing content
Set objTStream = objFSO.OpenTextFile (strTextFile, ForWriting)
For intLine = 0 To UBound (arrLines)
strThisLine = arrLines (intLine)
'Write out each line in turn as long as it's got a line number
If Len (strThisLine)> 4 Then objTStream.WriteLine Mid (strThisLine, 6)
Next
objTStream.Close
End If
...
HTML <TEXTAREA> control can be returned to the Value of additional characters, which depend on the original contents of HTML page format and set the WRAP attribute. In particular, should be the end delimiter in the ASP script "%>" written immediately after the </ TEXTAREA> tags, to prevent the increase of an additional carriage return symbols. Use:
%> </ TEXTAREA> <p>
Instead of:
%>
</ TEXTAREA> <P>
3. Additional content to a text file
When you click Append button, the file may have additional content, and modify the contents of the documents is similar to as shown in Figure 5-16. Difference is that in order to open the file additional documents and not to rewrite. OpenTextFile method call additional parameters to increase to prevent the specified file does not exist 2 create a new document.
...
If Len (Request.Form ( "cmdAppend")) Then
'append contents of TEXTAREA to file
strNewText = Request.Form ( "txtContent")
arrLines = Split (strNewText, vbCrLf)
Set objTStream = objFSO.OpenTextFile (strTextFile, ForAppending, False)
For intLine = 0 To UBound (arrLines)
strThisLine = arrLines (intLine)
If Len (strThisLine)> 4 Then objTStream.WriteLine Mid (strThisLine, 6)
Next
objTStream.Close
End If
...

Figure 5-16 added to the text file of the sample content page
4. Rewrite the default content
Finally, Restore the initial default button is used to rewrite the contents of a simple text file back to. Code and the methods used TextStream write a text file like this:
...
If Len (Request.Form ( "cmdDefault")) Then
'write out default contents to file
Set objTStream = objFSO.CreateTextFile (strTextFile, True, False)
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
End If

5.6 Summary
This chapter describes the page in the ASP objects and components used in the capacity of the powerful. Objects and components to first discuss the general characteristics, as well as their type. And then focus on how to ASP (and client) script code to create object instance.
Use the page number of the object may be "external" components, these components installed on the server, independent of the ASP. The object of discussion in this chapter, when using a default ASP scripting language (such as VBScript or JScript) always available. The realization of scrrun.dll document through the script run-time library completed.
Dictonary these objects is the object, FileSystemObject object and object TextStream.
Dictionary object store provides us with an effective way value can be indexed according to name and access, rather than figures based on a visit. This is the store name / value of such an ideal way of data.
TextStream object FileSystemObject objects and the links between each other closely, you can use them to access the server or network (mapped) in the directory of the disk drive. FileSystemObject object to provide the drive, folder (directory) and file access, and to provide for access to more information about or move, copy, delete their properties and methods.
Corresponds to the system can create any document TextStream object, and through the object to read and write files. Reading and writing process for it to operate as a text file, or even to deal with Unicode format. On the file system navigation and the combination of reading and writing skills on the server file system allows for extremely complex control. Can also be client-side script code in the use of objects (some restrictions apply).


Asp Base Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.