Javascript 讀寫文件示例:
- <script type="text/javascript" language="javascript">
- //讀文件
- 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('write ok');
- }
- </script>
- <html>
- <input type="text" id="in" name="in" />
- <input type="button" value="Write!" οnclick="writeFile('c:/12.txt',document.getElementById('in').value);"/>
- <br><br>
- <input type="button" value="Read!" οnclick="document.getElementById('show').value=readFile('c:/12.txt');"/>
- <br>
- <textarea id="show" name="show" cols="50" rows="8" >
- </textarea>
- </html>
<script type="text/javascript" language="javascript">
//讀文件
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('write ok');
}
</script>
<html>
<input type="text" id="in" name="in" />
<input type="button" value="Write!" οnclick="writeFile('c:/12.txt',document.getElementById('in').value);"/>
<br><br>
<input type="button" value="Read!" οnclick="document.getElementById('show').value=readFile('c:/12.txt');"/>
<br>
<textarea id="show" name="show" cols="50" rows="8" >
</textarea>
</html>
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 使用系統默認值打開文件。
- -->
<!--
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 使用系統默認值打開文件。
-->