ASP Getting Started Tutorial-reading text files

Source: Internet
Author: User
Tags expression readline

First, read the text file in the following steps:

1, create FileSystemObject object instance;

    <% Set fso=Server.CreateObject("Scripting.FileSystemObject") %>

2, using the OpenTextFile method of FileSystemObject object to return a TextStream object instance;

    <% Set txtFile=fso.OpenTextFile(filename[, iomode[, create[, format]]]) %>

3. Read the data from the file using the read, ReadLine, ReadAll, Skip, or SkipLine method of the TextStream object.

Note: Below we take the OpenTextFile method each parameter to make the annotation:

1) Txtfile: the instance name of a TextStream object returned by the OpenText method;

2) FSO: Required parameters, specify the name of the FileSystemObject object;

3 filename: Required parameter, is a string expression, used to specify the name of the file to open;

4) Ipmode: is an optional parameter that specifies the input/output mode of the file:

* If the parameter is 1 or omitted, the file is opened in read-only mode and cannot be written to;

* If the parameter is 2, open the file as write-only and you cannot read the file;

* If this parameter is 8, open the file and write at the end of the file.

5 Create is an optional parameter that takes a Boolean value indicating whether a new file can be created when the specified filename does not exist:

* If this argument is True, new files are allowed to be created;

* If this argument is False or omitted, creating a new file is not allowed.

6) format; is an optional parameter that specifies in what format the file is opened:

* If the parameter is 0 or omitted, open the file in ASCII format

* If-1, open the file in Unicode format;

* If this argument is-2, the file is opened in the system default format.

Second, the method used to read the file in the following table:

Methods of TextStream objects

Method Description/Expression/annotation
Read Reads the following num characters from the position of the file pointer and then stores them to the string.
Object. Read (characters)
The Parameter object specifies the name of the TextStream object, and the characters parameter specifies the number of strings to read from the file.
ReadAll Reads the entire file and then holds it to the string.
Object. ReadAll
The Parameter object specifies the name of the TextStream object.
ReadLine Reads a row from the position of the file pointer and then holds it to the string.
Object. ReadLine
The Parameter object specifies the name of the TextStream object.
Skip The Num characters are skipped when the file is read.
Object. Skip (characters)
The Parameter object specifies the name of the TextStream object, and the characters parameter specifies the number of strings to read from the file.
SkipLine Read a file skip a row.
Object. Skiplin
The Parameter object specifies the name of the TextStream object.

Note: 1 Skipping a line means reading and discarding all the characters in the bank, and an error occurs if the file is not opened in read mode.

2 After you perform a read-write operation on an open file, you should close the file by using the TextStream object's closing method.

Related Article

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.