FSO Use Tutorial 6--How to read files using the FSO

Source: Internet
Author: User
Tags error code readline
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
%>



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.