操作文件主要是利用了Javascript中的FileSystemObject对象,直接上代码:
<html>
<head>
<script>
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1, ForWriting = 2, ForAppending = 8;
function createFile(file){
var tf = fso.CreateTextFile(file, true);
tf.Close();
}
function readFileOnly(file){
var ts = fso.OpenTextFile(file, ForReading);
var s = ts.ReadAll();
ts.Close();
alert(s);
}
function readFileForWrite(file,content){
var ts = fso.OpenTextFile(file, ForWriting);
ts.Write(content);
ts.Close();
}
function readFileForAppend(file,content){
var ts = fso.OpenTextFile(file, ForAppending);
ts.Write(content);
ts.Close();
}
</script>
</head>
<body>
<input type="button" onclick="createFile('d:\\test.txt');" value="创建文件"/><br>
<input type="button" onclick="readFileOnly('d:\\test.txt');" value="读取文件"/><br>
<input type="button" onclick="readFileForWrite('d:\\test.txt','write');" value="覆盖文件内容"/><br>
<input type="button" onclick="readFileForAppend('d:\\test.txt','append');" value="追加到文件末尾"/>
</body>
</html>
文件的创建,读取和写入方法都有,可以参考。