教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 JS本地文件操作 JS读写txt文件示例

JS本地文件操作 JS读写txt文件示例

发布时间:2015-05-11   编辑:jiaochengji.com
分享一个js读写本地txt文件的代码,用js读取与写入文件,有需要的朋友参考下。

例子:
 

复制代码 代码示例:

//js读写文件,读取与写入文件
<script language=”javascript” type=”text/javascript”>
/*
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object 必选项。object 应为 FileSystemObject 的名称。
filename 必选项。指明要打开文件的字符串表达式。
iomode可选项。可以是三个常数之一:ForReading 、 ForWriting 或 ForAppending 。
create可选项。Boolean 值,指明当指定的 filename 不存在时是否创建新文件。如果创建新文件则值为 True ,如果不创建则为 False 。如果忽略,则不创建新文件。
format可选项。使用三态值中的一个来指明打开文件的格式。如果忽略,那么文件将以 ASCII 格式打开。
设置iomode 参数可以是下列设置中的任一种:
常数值描述
ForReading 1 以只读方式打开文件。不能写这个文件。
ForWriting 2 以写方式打开文件
ForAppending 8 打开文件并从文件末尾开始写。
format 参数可以是下列设置中的任一种:值描述TristateTrue 以 Unicode 格式打开文件。TristateFalse 以 ASCII 格式打开文件。TristateUseDefault 使用系统默认值打开文件。
*/

//读文件
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://12.txt’, document.getElementById(‘in’).value);”/>
<br>
<br>
<input type=”button” value=”Read!” onclick=”document.getElementById(‘show’).value=readFile(‘F:/Appserv/www/12.txt’);”/>
<br>
<textarea id=”show” name=”show” cols=”100″ rows=”20″ ></textarea>
</html>

您可能感兴趣的文章:
js读取与写入本地文件实现代码
JS本地文件操作 JS读写txt文件示例
jQuery【事件】问题
js读写文件实例代码(读取与写入文本文件)
Python3爬虫进阶:TXT文本存储
JS如何判断上传文件类型
C#清空添加的txt文件的实例代码
php文件操作的小例子
访问外部文本文件的vbs方法及例子
php 文件系统处理 fopen

关键词: 读取文件  写入文件   
[关闭]
~ ~