fso| Tutorial
How to use the FSO to read files -fso Use tutorial 6
The TextStream object provides three ways to read files: ReadLine, read, and ReadAll. Before calling these methods, you must be aware of the problem with the end of the file----when the contents of the entire file are read, if you call Readline,read, ReadAll, and so on, a "Enter the end of file" error ( error code =62) is generated.
So the better habit is to read the AtEndOfStream attribute value of the TextStream object before calling ReadLine, read, and ReadAll to determine whether the file has reached its end, and its procedures are as follows:
' txt is a TextStream object
If not Txt.atendofstream Then ' first determine where the end is not reached
' Call txt. ReadLine, Read, ReadAll and other methods
End If
Six, how to use the FSO to read files:
Method 1:readline
Calling format: (reading one row of data from a file)
' txt is a TextStream object
If not Txt.atendofstream Then ' first determine where the end is not reached
line = txt. ReadLine ' reads a row of data
Response.Write Line & "<br>"
End If
Method 2:readall
Call format: (read all the contents of the file one at a time)
' txt is a TextStream object
If not Txt.atendofstream Then ' first determine where the end is not reached
Content = txt. ReadAll ' reads the entire file's data
Lines = replace (Content, VbCrlf, "<br>") ' converts text-line character VbCrlf to HTML newline tag ' <br> '
Response.Write Lines
End If
Method 3:read (N)
Call format: (reads n bytes of data from a file)
' txt is a TextStream object
If not Txt.atendofstream Then ' first determine where the end is not reached
Content = txt. Read (30) ' reads 30 bytes of data
Response.Write Content
End If
SOURCE Example 1: (using the ReadAll method to read File1.txt content and display it)
<%
'==================================================
' Author: Arisisi
' URL:http://www.alixixi.com/
' Source: FSO Read all the contents of the File sample
' Time: December 17, 2005
'==================================================
Set fs = Server.CreateObject ("Scripting.FileSystemObject")
File = Server.MapPath ("File1.txt")
Set txt = fs. OpenTextFile (File)
If not Txt.atendofstream Then ' first determine where the end is not reached
Content = txt. ReadAll ' reads the entire file's data
Lines = replace (Content, VbCrlf, "<br>") ' converts text-line character VbCrlf to HTML newline tag ' <br> '
Response.Write Lines
End If
%>
SOURCE Example 2: (using the ReadLine method to read File1.txt row of data content and display it)
<%
'================================================
' Author: Arisisi
' URL:http://www.alixixi.com/
' Source: FSO read a file line sample
' Time: December 17, 2005
'================================================
Set fs = Server.CreateObject ("Scripting.FileSystemObject")
File = Server.MapPath ("File1.txt")
Set txt = fs. OpenTextFile (File)
If not Txt.atendofstream Then ' first determine where the end is not reached
line = txt. ReadLine ' reads a row of data
Response.Write Line & "<br>"
End If
%>