js读写txt文件
Posted Nicolasap
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js读写txt文件相关的知识,希望对你有一定的参考价值。
view plain
<script language="javascript" type="text/javascript">
//读文件
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‘);
}
</script>
<html>
<input type="text" id="in" name="in" />
<input type="button" value="Write!" onclick="writeFile(‘F:/Appserv/www/12.txt‘,document.getElementByIdx_x(‘in‘).value);"/><br><br>
<input type="button" value="Read!" onclick="document.getElementByIdx_x(‘show‘).value=readFile(‘F:/Appserv/www/12.txt‘);"/><br>
<textarea id="show" name="show" cols="100" rows="20" >
</textarea>
</html>
<script language="javascript" type="text/javascript">
//读文件
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‘);
}
</script>
<html>
<input type="text" id="in" name="in" />
<input type="button" value="Write!" onclick="writeFile(‘F:/Appserv/www/12.txt‘,document.getElementByIdx_x(‘in‘).value);"/><br><br>
<input type="button" value="Read!" onclick="document.getElementByIdx_x(‘show‘).value=readFile(‘F:/Appserv/www/12.txt‘);"/><br>
<textarea id="show" name="show" cols="100" rows="20" >
</textarea>
</html>
以上是关于js读写txt文件的主要内容,如果未能解决你的问题,请参考以下文章