1. Write
FileSystemObject can translate files into a file stream.
First step:
Cases:
Copy Code code as follows:
Var fso=new ActiveXObject (Scripting.FileSystemObject);
Creates an object that can be translated into a file stream.
Step two: Use to create a TextStream object
There are three attributes inside the brackets
1. Absolute path to File
2. The constant number of files is read = 1 only, write = 2, append =8 and so on. (ForReading, ForWriting, or forappending.) );
3. A Boolean value allows the new to be true instead of false;
Cases:
Copy Code code as follows:
Var f=fso.createtextfile ("C:\a.txt", 2,true);
Step three: Call the TextStream method
1. Write (do not add a new line break at the end of the write data)
2. WriteLine (to add a new line break at the end)
3. WriteBlankLines (add one or more blank lines)
Cases:
Copy Code code as follows:
F.writeline ("Wo shi di yi hang");
Fourth Step:
Close the TextStream object:
Example: F.close ();
2. Read
First step:
Copy Code code as follows:
Var fso=new ActiveXObject (Scripting.FileSystemObject);
Creates an object that can be translated into a file stream.
Step two: Use to create a TextStream object
There are three attributes inside the brackets
4. Absolute path to File
5. The constant number of files is read = 1 only, write = 2, append =8 and so on. (ForReading, ForWriting, or forappending.) );
6. A Boolean value allows the new to be true instead of false;
Cases:
Copy Code code as follows:
Var f=fso.opentextfile ("C:\a.txt", 1,true);
Step three: Invoke the Read method
1. Read (used to read a specified number of characters in a file)
2. ReadLine (reads an entire line, but does not include line breaks)
3. ReadAll (then read the entire contents of the text file);
Determines whether to read to the last row
Copy Code code as follows:
while (!f.atendofstream)
{
F.readline ();
}
Fourth Step:
Close the TextStream object:
Example: F.close ();
The following is an example of an HTML open TXT file.
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv=" Content-type " Content= "text/html; Charset=utf-8 "/>
<title> Hello </title>
<body>
<div id=" AA "></div>
<script language=" JavaScript ">
var fso, TS, S;
var ForReading = 1;
FSO = new ActiveXObject ("Scripting.FileSystemObject");
ts = fso. OpenTextFile ("D:\\testfile.txt", ForReading);
s = ts. ReadLine ();
document.getElementById ("AA"). Innerhtml=s;
</script>
</body>