JavaScript 读取、写入Txt文档
<html>
<head>
<title>JS操作文本文件</title>
</head>
<body>
<script>
var arr = new Array();
arr[0] = [0,"name0","value0"];
arr[1] = [1,"name1","value1"];
arr[2] = [2,"name2","value2"];
arr[3] = [3,"name3","value3"];
arr[4] = [4,"name4","value4"];
function Write2Text()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.CreateTextFile("a.txt", true);
for(var i=0;i<arr.length;i++)
{
f.write(arr[i])
f.WriteBlankLines(1)
}
f.Close();
}
</script>
<input type=button value="Write" onclick="Write2Text()">
<script>
function GetHeader(src) {
var ForReading=1;
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso.OpenTextFile(src,ForReading);
return(f.ReadAll());
}
function ReadText(){
var arr=GetHeader("a.txt").split("\r\n");
for(var i=0;i<arr.length;i++){
alert("第"+(i+1)+"行数据为:"+arr[i]);
}
}
</script>
<input type=button value="Read" onclick="ReadText()">
</body>
</html>
<head>
<title>JS操作文本文件</title>
</head>
<body>
<script>
var arr = new Array();
arr[0] = [0,"name0","value0"];
arr[1] = [1,"name1","value1"];
arr[2] = [2,"name2","value2"];
arr[3] = [3,"name3","value3"];
arr[4] = [4,"name4","value4"];
function Write2Text()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.CreateTextFile("a.txt", true);
for(var i=0;i<arr.length;i++)
{
f.write(arr[i])
f.WriteBlankLines(1)
}
f.Close();
}
</script>
<input type=button value="Write" onclick="Write2Text()">
<script>
function GetHeader(src) {
var ForReading=1;
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso.OpenTextFile(src,ForReading);
return(f.ReadAll());
}
function ReadText(){
var arr=GetHeader("a.txt").split("\r\n");
for(var i=0;i<arr.length;i++){
alert("第"+(i+1)+"行数据为:"+arr[i]);
}
}
</script>
<input type=button value="Read" onclick="ReadText()">
</body>
</html>