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!