[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!" onclick="writeFile('c:/12.txt',document.getElementById('in').value);"/> <br><br> <input type="button" value="Read!" onclick="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!" onclick="writeFile('c:/12.txt',document.getElementById('in').value);"/> <br><br> <input type="button" value="Read!" onclick="document.getElementById('show').value=readFile('c:/12.txt');"/> <br> <textarea id="show" name="show" cols="50" rows="8" > </textarea> </html> Javascript 函數解釋:[javascript] view plaincopyprint? [javascript] view plaincopyprint?<!-- 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 使用系統預設值開啟檔案。 --> 運行介面(IE9): 讀寫測試: 首先,在上面運行介面,Write 按鈕前面的輸入框,填入文本“http://blog.csdn.net/sunboy_2050” 然後,點擊 Write 按鈕,寫入到檔案中(代碼預設寫到了C:\\12.txt) 最後,點擊 Read 按鈕,讀取剛才寫入C:\\12.txt的檔案內容,如 註:上述Javascript讀寫檔案代碼,只限使用IE核心瀏覽器,如IE、360瀏覽器、遨遊等,chrome、firefox、opera均無法實現讀寫