Home >Web Front-end >JS Tutorial >Javascript writing txt and reading txt file example_javascript skills

Javascript writing txt and reading txt file example_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:00:222098browse

1. Write

FileSystemObject to translate the file into a file stream.

Step one:

Example:

Copy code The code is as follows:

Var fso=new ActiveXObject(Scripting.FileSystemObject);

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

Step 2: Used to create a textStream object

There are three attributes in the brackets

1. The absolute path of the file

2. The constant of the file Read only=1, write only=2, append=8 and other permissions. (ForReading, ForWriting or ForAppending.);

3. A Boolean value is true if new creation is allowed, otherwise false;

Example:
Copy code The code is as follows:

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

Step 3: Call the textStream method

1. Write (without adding a new line break at the end of the written data)

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

3. WriteBlankLines (add one or more blank lines)

Example:
Copy code The code is as follows:

f.writeLine(“wo shi di yi hang”);

Step 4:

Close the textStream object :

Example: f.close();

2. Reading

Step 1:
Copy the code The code is as follows:

Var fso=new ActiveXObject(Scripting.FileSystemObject);

Create a file that can be translated into File stream object.

Step 2: Used to create a textStream object

There are three attributes in the brackets

4. The absolute path of the file

5. The constants of the file Read only=1, write only=2, append=8 and other permissions. (ForReading, ForWriting or ForAppending.);

6. A Boolean value is true if new creation is allowed, otherwise false;

Example:
Copy code 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 (read a whole line, but Excluding line breaks)

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

Determine whether the last line has been read
Copy code The code is as follows:

while (!f.AtEndOfStream)
{
f.Readline();
}

Step 4:

Close textStream object:

Example: f.close();

The following is an html to open a txt file Example.
Copy code The code is as follows:

.org/1999/xhtml">


Hello










Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn