FileSystemObject的TextStream物件 - 小瓜瓜
[首頁]
●先由FileSystemObject來建立TextStream物件:
Private Sub Command1_Click() - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 8, True)
End Sub |
●HTML、HTA下寫法一樣
<script language=vbscript> - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 8, True)
</script> |
●ASP下寫法也一樣
<% - Set FSysObj = Server.CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 8, True)
%> |
●建立好TextStream後,就可以對(duì)檔案做讀與寫的動(dòng)作。
●說明:8指的是附加模式,2為覆蓋模式,1為讀檔模式,True指的是檔案若不存在則建立該檔案。
●TextStream.Write與TestStream.WriteLine:這兩個(gè)都是把資料寫到開起的檔案,只不過WriteLine寫完資料後會(huì)自動(dòng)補(bǔ)上vbCrLf。
Private Sub Command1_Click() - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 2, True)
- TStrmObj.Write "hi,"
- TStrmObj.WriteLine "hello"
End Sub |
●TextStream.Read、TestStream.ReadLine與TextStream:這三個(gè)都是把資料從檔案裡讀出來,Read是讀取某幾個(gè)字,ReadLine是讀取一行而ReadAll則是讀入整個(gè)檔案。
Private Sub Command1_Click() - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 1, True)
- MsgBox TStrmObj.Read(2) '讀兩個(gè)字
End Sub |
●TextStream.Read、TestStream.ReadLine與TextStream:這三個(gè)都是把資料從檔案裡讀出來,Read是讀取某幾個(gè)字,ReadLine是讀取一行而ReadAll則是讀入整個(gè)檔案。
<script language="vbscript"> - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 1, True)
- Document.Write Replace(TStrmObj.ReadAll,vbCrLf,"<br />") '整個(gè)檔案讀出來
</script> |
●TextStream.AtEndOfStream可傳回是否已經(jīng)讀到檔尾:
Private Sub Command1_Click() - Set FSysObj = CreateObject("Scripting.FileSystemObject")
- Set TStrmObj = FSysObj.OpenTextFile("C:\Test.txt", 1, True)
-
Do Until TStrmObj.AtEndOfStream = True - MsgBox TStrmObj.Read(1)
Loop End Sub |