JavaScript Read txt text file method detailed

Source: Internet
Author: User

Http://blog.163.com/[email protected]/blog/static/8304612620122834121264/

The first step: Create an object that can translate the file into a file stream.
Var fso=new ActiveXObject (Scripting.FileSystemObject);


Step two: Used to create a TextStream object with three properties inside the parentheses
1. Absolute path to the file
2. The file's constant number is read Only = 1, write only = 2, append =8 and other permissions. (ForReading, ForWriting, or forappending. );
3. A Boolean value that allows new is true instead of false;
Example: Var f=fso.opentextfile ("C:\a.txt", 1,true);


Step three: Call the Read method
1. Read (for reading a specified number of characters in a file)
2. ReadLine (reads a whole line, but does not include newline characters)
3. ReadAll (then reads the entire contents of the text file);
Determine if the last line is read
while (!f.atendofstream)
{
F.readline ();
}


Fourth Step:
To close the TextStream object:
Example: F.close ();

Example:
<title>new page</title>
<script>
function GetHeader (SRC) {
Permission Read Only (read = 1, write only = 2, append =8, etc.)
var forreading=1;
var fso=new activexobject ("Scripting.FileSystemObject");
var f=fso. OpenTextFile (src,forreading);
Return (F.readall ());
}
var arr=getheader ("D:\\test.txt"). Split ("\ r \ n");
for (Var i=0;i<arr.length;i++) {
Alert ("the" + (i+1) + "line data is:" +arr[i]);
}
</script>
<body>
</body>

JavaScript Read txt text file method detailed

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.