asp FSO 讀寫檔案本檔案實現代碼

來源:互聯網
上載者:User

1.AtEndOfStream

該屬性工作表明是否已到達整個文字檔末尾。其值為"TRUE"或"FALSE"

2.CreateTextFile

用來建立新的文字檔

3.OpenTextFile()方法中的參數

saucer(思歸)所寫的:

 代碼如下 複製代碼
Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)

ForWriting值為2,表示開啟一個可寫的文字檔
為1,表示開啟一個可讀的文字檔
為8,表示要附加資料

True是表示如果沒有這個檔案,將建立一個
這個參數可選True或False,表明檔案不存在時是否建立

執行個體1

讀檔案:

 代碼如下 複製代碼
<%
set myfileobject=server.createobject("scripting.filesystemobject")
set mytextfile=myfileobject.opentextfile("c:mydirtest.txt")
while not mytextfile.atendofstream
  response.write(mytextfile.readline)
wend
mytextfile.close
%>


寫檔案:

 代碼如下 複製代碼
<%
set myfileobject=server.CreateObject("Scripting.FileSystemObject")
set mytextfile=myfileobject.CreateTextFile("c:mydirtest.txt")
mytextfile.WriteLine
mytextfile.close
%>


執行個體2

FSO打造最簡單的訪問計數器

 代碼如下 複製代碼

<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
File = Server.MapPath("counter.txt")
Set txtr = fs.OpenTextFile(File,1,true)'開啟唯讀檔案file,如果不存在則建立
counter=0'將計數器歸零
If Not txtr.atEndOfStream Then'先確定還沒有到達結尾的位置
Line = txtr.ReadLine '讀取一行資料
else
line=0 '否則設定初始值為0
End If
counter=line+1 '計數加1
set txtw = fs.opentextfile(file,2,true) '開啟唯寫檔案file
txtw.write counter '寫入計數
response.Write("<center>您是第<b>"&counter&"</b>位訪客!</center>")
'輸出計數
%>

在需要統計和顯示計數的地方可以用include的方式引入這個文檔。

 代碼如下 複製代碼

<!--#include file="counter.asp"-->


但是如果是html之類靜態頁面,則可以通過script指令碼的方式來使用這個簡單的計數器。

那麼Counter.asp則需要這麼寫了

 代碼如下 複製代碼

<%

response.Write("document.write(""<center>您是第<b>"&counter&"</b>位訪客!</center>"");")

%>

需要引用的html靜態頁裡可以通過Script指令碼來使用計數器:

 代碼如下 複製代碼

<script src="counter.asp"></script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.