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!