Example _ Javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to write and read txt files in Javascript. For more information, see section 1. Write

FileSystemObject can translate a file into a file stream.

Step 1:

Example:

The Code is as follows:


Var fso = new ActiveXObject (Scripting. FileSystemObject );


Create an object that can translate a file into a file stream.

Step 2: Create a textStream object

There are three attributes in the brackets.

1. absolute path of the file

2. The constant of the file is read-only = 1, write-only = 2, and append = 8. (ForReading, ForWriting, or ForAppending .);

3. If a Boolean value is allowed to be created, the opposite value is true;

Example:

The Code is as follows:


Var f = fso. createtextfile ("C: \ a.txt", 2, true );


Step 3: Call the textStream Method

1. Write (do not add a new line break at the end of the written data)

2. WriteLine (Add a new line break at the end)

3. WriteBlankLines (add one or more empty lines)

Example:

The Code is as follows:


F. writeLine ("wo shi di yi hang ");


Step 4:

Disable textStream object:

Example: f. close ();

2. Read

Step 1:

The Code is as follows:


Var fso = new ActiveXObject (Scripting. FileSystemObject );


Create an object that can translate a file into a file stream.

Step 2: Create a textStream object

There are three attributes in the brackets.

4. Absolute file path

5. The constant of the file is read-only = 1, write-only = 2, and append = 8. (ForReading, ForWriting, or ForAppending .);

6. If a Boolean value is allowed to be created, the opposite value is false if the value is true;

Example:

The Code is as follows:


Var f = fso. opentextfile ("C: \ a.txt", 1, true );


Step 3: Call the read Method

1. Read (used to Read the specified number of characters in the file)

2. ReadLine)

3. ReadAll (reads the entire content of a text file );

Judge whether to read the last row

The Code is as follows:


While (! F. AtEndOfStream)
{
F. Readline ();
}


Step 4:

Disable textStream object:

Example: f. close ();

The following is an example of opening a txt file in html.

The Code is as follows:






Hi!





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.