In FSO, apart from the operations on the drive and folder, the most powerful function is the operations on the file. It can be used to count, manage content, search, and generate dynamic HTML pages.
I. fso. OpenTextFile
Needless to say, fso. OpenTextFile is used to open a file, which is generally a txt text file. Therefore, first create a txt file and then read the content through FSO.
1. Info.txt
Name: cnbruce
Sex: male
After this file is created, create an ASP page. Of course, it is best that the two files are in the same directory.
2. opentxt. asp
<%
Whichfile = server. mappath ("info.txt ")
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set txt = fso. OpenTextFile (whichfile, 1)
Rline = txt. ReadLine
Rline = rline & "<br>" & txt. ReadLine
Response. Write rline
Txt. Close
%>
Note: whether you use FSO to open a drive, open a folder, open a file, or open a database to be accessed later, you can only open an absolute physical path address. However, it is generally uploaded to the space service provider, so you cannot directly learn the location of your file. Therefore, we strongly recommend that you use the server. mappath method, which is highly portable and applicable.
CreateObject ("Scripting. FileSystemObject" creates a connection to the fsogroup. Fso.opentextfile(whichfile,1)open info.txt. Parameter "1" indicates "ForReading: open the file in read-only mode. This file cannot be written .", The other parameter "2" indicates "ForWriting: open the file in write mode", and parameter "8" indicates "ForAppending: open the file and start writing from the end of the file ".
After opening the file, do you want to display the content in the file? Use the txt. ReadLine method to read the entire line of text. If you want to continue reading the next line, continue to use the txt. ReadLine method. Of course, there are other reading methods at the beginning. For example, txt. Read (7) reads a specified number of characters and txt. ReadAll returns all the content in the text.
II. fso. CreateTextFile
For example, if fso. CreateFolder creates a folder, fso. CreateTextFile creates a file.
3. creattxt. asp
<%
Whichfile = server. mappath ("info.txt ")
Set fso = CreateObject ("Scripting. FileSystemObject ")
Set MyFile = fso. CreateTextFile (whichfile, True)
MyFile. WriteLine ("My Name Is CN-Bruce ")
MyFile. WriteLine ("My Sex Is Male ")
MyFile. Close
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.