Home  >  Article  >  Web Front-end  >  Javascript reading and writing file example code sharing

Javascript reading and writing file example code sharing

迷茫
迷茫Original
2017-03-26 17:38:331721browse

Integrated the online code for JS to read and write files, but this function can only be executed under the IE browser, and IE on some computers needs to be set.

The following is the writing code:

var fso = new ActiveXObject("Scripting.FileSystemObject");
//创建一个可以将文件翻译成流的对象
var f = fso.createtextfile("filename.txt",2,"true");
//创建一个textSteam对象,第一个参数为文件的绝对路径,第二个参数为权限(1=只读=ForReading,2=只写=ForWriting,8=追加=ForAppending),第三个参数为是否允许新建
f.write("content");//将content写入文件。(不在数据末尾添加换行符)
f.writeLine("content");//在数据末尾添加换行符
f.close();//关闭textSteam对象

The following is the reading code:

var fso = new ActiveXObject("Scripting.FileSystemObject");
//创建一个可以将文件翻译成流的对象
var f = fso.opentextfile("filename.txt",1,"true");
//创建一个textStream对象,参数同上
f.read(num);
//读取文件中指定数量的字符,比如f.read(2),一个中文的数量也为1
f.readLine();
//读取一整行,不包括换行符
f.readAll();
//读取文件整个内容
f.close();
//关闭textStream对象

The above is the detailed content of Javascript reading and writing file example code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn