js操作文件

 

 1 <html>
 2 <head>
 3 <script>
 4 var fso = new ActiveXObject("Scripting.FileSystemObject");
 5 var ForReading = 1, ForWriting = 2, ForAppending = 8;
 6 function createFile(file){
 7    var tf = fso.CreateTextFile(file, true);
 8    tf.Close();
 9 }
10 function readFileOnly(file){
11    var ts = fso.OpenTextFile(file, ForReading);
12    var s = ts.ReadAll();
13    ts.Close();
14    alert(s);
15 }
16 function readFileForWrite(file,content){
17    var ts = fso.OpenTextFile(file, ForWriting);
18    ts.Write(content);
19    ts.Close();
20 }
21 function readFileForAppend(file,content){
22    var ts = fso.OpenTextFile(file, ForAppending);
23    ts.Write(content);
24    ts.Close();
25 }
26 </script>
27 </head>
28 <body>
29     <input type="button" onclick="createFile('d:\\test.txt');" value="创建文件"/><br>
30     <input type="button" onclick="readFileOnly('d:\\test.txt');" value="读取文件"/><br>
31     <input type="button" onclick="readFileForWrite('d:\\test.txt','write');" value="覆盖文件内容"/><br>
32     <input type="button" onclick="readFileForAppend('d:\\test.txt','append');" value="追加到文件末尾"/>
33 </body>
34 </html>

 

posted @ 2012-11-21 14:47  高 明  阅读(4148)  评论(0编辑  收藏  举报