js如何读取文件夹文件

JavaScript013

js如何读取文件夹文件,第1张

JS读写文件,分别如下:

//读文件

function readFile(filename){

var fso = new ActiveXObject("Scripting.FileSystemObject")

var f = fso.OpenTextFile(filename,1)

var s = ""

while (!f.AtEndOfStream)

s += f.ReadLine()+"\n"

f.Close()

return s

}

//读文件

function readFile(filename){

var fso = new ActiveXObject("Scripting.FileSystemObject")

var f = fso.OpenTextFile(filename,1)

var s = ""

while (!f.AtEndOfStream)

s += f.ReadLine()+"\n"

f.Close()

return s

}

//写文件

function writeFile(filename,filecontent){

var fso, f, s

fso = new ActiveXObject("Scripting.FileSystemObject")

f = fso.OpenTextFile(filename,8,true)

f.WriteLine(filecontent)

f.Close()

alert('ok')

}

使用jquery 获取相应的元素非常简便。

// 获取第一个p标签

const firstP = $('p:first')

// 获取最后一个p标签

const lastP = $('p:last')

console.log(firstP, lastP)

,参考例子如下:

<html>

<head>

<title>JS操作文本文件</title>

</head>

<body>

<script>

var arr = 1

function Write2Text()

{

var fso = new ActiveXObject("Scripting.FileSystemObject")

var f = fso.CreateTextFile("a.txt", true)

f.write(arr)

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=0i<arr.lengthi++){

alert("第"+(i+1)+"行数据为:"+arr[i])

}

}

</script>

<input type=button value="Read" onclick="ReadText()">

</body>

</html>