因此,可以使用這些方法創(chuàng)建一個(gè)新的文本文件,或者打開一個(gè)已存在的文件。則可得到與該文件相應(yīng)的一個(gè)TextStream對(duì)象,可以使用TextStream對(duì)象的屬性和方法操作文件。 1. 創(chuàng)建新的文本文件 可以用CreateTextFile方法創(chuàng)建新的文本文件,或覆蓋一個(gè)已存在的文件。返回的TextStream對(duì)象可用來讀寫文件。 首先創(chuàng)建一個(gè)FileSystemObject對(duì)象,用來創(chuàng)建TextStream對(duì)象。下面這個(gè)例子是用VBScript創(chuàng)建一個(gè)“普通的”(即非Unicode)名為MyFile.txt的文件,并覆蓋已存在的同名文件: Set objfso = Server.CreateObject("Scripting.FileSystemObject") Set objTStream = objFSO.CreateTextFile("C:/TextFiles/MyFile.txt", True, False) 這同樣可用JScript實(shí)現(xiàn): var objFSO = Server.CreateObject('Scripting.FileSystemObject'); var objTStream = objFSO.CreateTextFile('C:/TextFiles/MyFile.txt', true, false); 一旦創(chuàng)建了該文件,就可以使用objTStream(它是對(duì)一個(gè)TextStream對(duì)象的引用)對(duì)文件進(jìn)行操作。 2. 打開已存在的文本文件 OpenTextFile方法用于打開一個(gè)已有的文本文件。它返回一個(gè)TextStream對(duì)象,可用這個(gè)對(duì)象對(duì)文件讀或追加數(shù)據(jù)。 同樣,首先創(chuàng)建一個(gè)FileSystemObject對(duì)象,然后用其創(chuàng)建一個(gè)TextStream對(duì)象。下面的VBScript程序例子打開一個(gè)名為MyFile.txt的文件,準(zhǔn)備讀出其內(nèi)容: Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objTStream = objFSO.OpenTextFile("C:/TextFiles/MyFile.txt", ForReading) 用JScript: var objFSO = Server.CreateObject('Scripting.FileSystemObject'); var objTStream = objFSO.OpenTextFile('C:/TextFiles/MyFile.txt', ForReading); 為了寫入一個(gè)文件或創(chuàng)建一個(gè)不存在的文件,可以用以下代碼: ' In VBScript: Set objTStream = objFSO.OpenTextFile("C:/TextFiles/MyFile.txt", ForWriting, True)
// In JScript: var objTStream = objFSO.OpenTextFile('C:/TextFiles/MyFile.txt', ForWriting, true); 如果要打開一個(gè)已有的Unicode文件,準(zhǔn)備對(duì)其追加數(shù)據(jù),但是不創(chuàng)建不存在的文件,可以用: ' In VBScript: Set objTStream = objFSO.OpenTextFile("C:/TextFiles/MyFile.txt", ForReading, _ False, TristateTrue) // In JScript: var objTStream = objFSO.OpenTextFile('C:/TextFiles/MyFile.txt', ForReading, _ fasle, TristateTrue); 3. 作為一個(gè)TextStream對(duì)象打開一個(gè)File對(duì)象 可用File對(duì)象的OpenAsTextStream方法打開與該對(duì)象相應(yīng)的文件,并且返回一個(gè)能對(duì)該文件進(jìn)行讀、寫和追加的TextStream對(duì)象。所以,給定一個(gè)File對(duì)象(這種情況下不是FileSystemObject對(duì)象)——objFileObject,可作為一個(gè)“普通的”(非Unicode)TextStream對(duì)象打開它,以供追加文件內(nèi)容: ' In VBScript: Set objTStream = objFileObject.OpenAsTextStream(ForAppending, False)
// In JScript: var objTStream = objFileObject.OpenTextFile(ForAppending, false); 注意,使用這種方法不需要文件名,因?yàn)槌绦虻膱?zhí)行通過引用File對(duì)象進(jìn)行,并且沒有create參數(shù),因?yàn)樵撐募仨氁汛嬖冢绻霃囊粋€(gè)新的空的文件開始,可以用: ' In VBScript: Set objTStream = objFileObject.OpenAsTextStream(ForWriting)
// In JScript: var objTStream = objFileObject.OpenTextFile(ForWriting); 如果想讀取該文件: ' In VBScript: Set objTStream = objFileObject.OpenAsTextStream(ForReading)
// In JScript: var objTStream = objFileObject.OpenTextFile(ForReading);
3. 寫文本文件 一旦使用CreateTextFile、OpenTextFile或OpenAsTextStream方法以及ForWriting或ForAppending參數(shù),創(chuàng)建一個(gè)對(duì)應(yīng)于某個(gè)文件的TextStream對(duì)象,可以用下面的VBScript程序?qū)懳募完P(guān)閉文件: ' In VBScript: objTStream.WriteLine "At last I can create files with VBScript!" objTStream.WriteLine objTStream.WriteLine "Here are three blank lines:" objTStream.WriteBlankLines 3 objTStream.Write "... and this is " objTStream.WriteLine "the last line." objTStream.Close 或者用JScript: // In JScript: objTStream.WriteLine('At last I can create files with JScript! '); objTStream.WriteLine(); objTStream.WriteLine('Here are three blank lines: '); objTStream.WriteBlankLines(3); objTStream.Write('... and this is '); objTStream.WriteLine('the last line. '); objTStream.Close(); 4. 讀文本文件 一旦使用CreateTextFile、OpenTextFile或OpenAsTextStream方法以及ForReading參數(shù),創(chuàng)建一個(gè)對(duì)應(yīng)于某個(gè)文件的TextStream對(duì)象,可以用下面的VBScript程序讀文件和關(guān)閉文件: ' In VBScript: 'read one line at a time until the end of the file is reached Do While Not objTStream.AtEndOfStream 'get the line number intLineNum = objTStream.Line 'format it as a 4-character string with leading zeros strLineNum = Right("000" & CStr(intLineNum), 4) 'get the text of the line from the file strLineText = objTStream.ReadLine Response.Write strLineNum & ": " & strLineText & "<BR>" Loop objTStream.Close 或用JScript: // In JScript: // read one line at a time until the end of the file is reached while (! objTStream.AtEndOfStream) { // get the line number intLineNum = objTStream.Line; // format and convert to a string strLineNum = '000' + intLineNum.toString(); strLineNum = substr(strLineNum, strLineNum.length – 4, 4) // get the text of the line from the file strLineText = objTStream.ReadLine(); Response.Write(strLineNum + ': ' + strLineText + '<BR>'); } objTStream.Close();
5.5.3 TextStream對(duì)象舉例 為了了解使用TextStream對(duì)象操作磨滅文件的幾種方式,本書提供了一個(gè)VBScript示例頁(yè)面,該頁(yè)使用了大量的上述的代碼。從示例的Chapter05主菜單頁(yè),選擇鏈接“Working With the TextStream Object”打開show_textstream.asp頁(yè)面。 該頁(yè)顯示了存儲(chǔ)在磁盤上名為MyFile.txt的文件的文本內(nèi)容。在<TEXTAREA>控件里顯示的文本內(nèi)容允許進(jìn)行編輯,并且下面有三個(gè)按鈕。三個(gè)按鈕的作用分別是用<TEXTAREA>控件的內(nèi)容更新(即取代)最初的文本,在已有文件內(nèi)容的后面添加文本,或用初始的缺省內(nèi)容重寫文件,如圖5-15所示:
'create an instance of a FileSytemObject object Set objFSO = Server.CreateObject("Scripting.FileSystemObject") … 與書中本部分的大多數(shù)其他示例一樣,該頁(yè)包含一個(gè)<FORM>段以保存該頁(yè)面的HTML控件。ACTION是當(dāng)前頁(yè)面,因此窗體的內(nèi)容送回到同一頁(yè)面。 每次載入該頁(yè)面,<TEXTAREA>控件用文本文件的當(dāng)前內(nèi)容填充: … <FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST">
The contents of the disk file <B><% = strTextFile %></B> are:<P> <TEXTAREA NAME="txtContent" ROWS="10" COLS="50" WRAP="PHYSICAL"> <% 'open the text file as a TextStream object Set objTStream = objFSO.OpenTextFile(strTextFile, ForReading) 'read one line at a time until the end of the file is reached Do While Not objTStream.AtEndOfStream 'get the line number intLineNum = objTStream.Line 'format and convert to a string strLineNum = Right("00" & CStr(intLineNum), 3) 'get the text of the line from the file strLineText = objTStream.ReadLine Response.Write strLineNum & ": " & strLineText & vbCrLf Loop objTStream.Close %> </TEXTAREA><P> 由上面程序可知道如何打開文本文件進(jìn)行讀取,遍歷整個(gè)文件每次讀取一行(而不是作為一個(gè)字符串讀出整個(gè)文件)。這是因?yàn)橐砑幼约旱男刑?hào),行號(hào)不屬于該文件的文本。對(duì)從該文件讀出的每行(讀之前),檢索并且格式化Line屬性并創(chuàng)建一個(gè)三位數(shù)字的行號(hào)。然后把行編號(hào)和文本行放置頁(yè)面的<TEXTAREA>控件內(nèi)。 2. 更新文本文件的內(nèi)容 當(dāng)點(diǎn)擊頁(yè)面的Update按鈕時(shí)(一般是在編輯了<TEXTAREA>控件里的文本以后),將把<TEXTAREA>控件里的內(nèi)容重新寫入到該文本文件內(nèi)。為此,該頁(yè)有相應(yīng)的一些ASP代碼,在創(chuàng)建HTML控件以前檢驗(yàn)Request.Form集合,查看點(diǎn)擊的是哪一個(gè)按鈕(如果有的話),然后就重新載入該頁(yè)。 如果點(diǎn)擊Update按鈕,搜集<TEXTAREA>控件的內(nèi)容作為一個(gè)字符串,分離這個(gè)字符串使之成為獨(dú)立文本選擇的數(shù)組,并且打開文本文件準(zhǔn)備重寫其內(nèi)容,然后遍歷剛剛創(chuàng)建的數(shù)組,按行號(hào)循環(huán)寫入該行的內(nèi)容: … 'look for a command sent from the FORM section buttons If Len(Request.Form("cmdUpdate")) Then 'Get contents of TEXTAREA control strNewText = Request.Form("txtContent") 'Split it into an array of lines at each carriage return arrLines = Split(strNewText, vbCrLf) 'Open the text file for writing, which replaces all existing content Set objTStream = objFSO.OpenTextFile(strTextFile, ForWriting) For intLine = 0 To UBound(arrLines) strThisLine = arrLines(intLine) 'Write out each line in turn as long as it’s got a line number If Len(strThisLine) > 4 Then objTStream.WriteLine Mid(strThisLine, 6) Next objTStream.Close End If … HTML<TEXTAREA>控件可在返回的Value中增加額外字符,這依賴于原始HTML頁(yè)內(nèi)的內(nèi)容格式和WRAP屬性的設(shè)置。特別是應(yīng)在ASP腳本結(jié)束定界符“%>”后立即寫上</TEXTAREA>標(biāo)記,以防止增加一個(gè)額外的回車符號(hào)。即使用: %></TEXTAREA><p> 而不使用: %> </TEXTAREA><P> 3. 向文本文件追加內(nèi)容 當(dāng)點(diǎn)擊Append按鈕時(shí),可對(duì)已有的文件追加內(nèi)容,與修改該文件內(nèi)容類似,如圖5-16所示。區(qū)別是打開該文件是為了追加而不是為了改寫文件。調(diào)用OpenTextFile方法時(shí)可增加額外參數(shù),防止在指定的文件2不存在時(shí)創(chuàng)建新文件。 … If Len(Request.Form("cmdAppend")) Then 'append contents of TEXTAREA to file strNewText = Request.Form("txtContent") arrLines = Split(strNewText, vbCrLf) Set objTStream = objFSO.OpenTextFile(strTextFile, ForAppending, False) For intLine = 0 To UBound(arrLines) strThisLine = arrLines(intLine) If Len(strThisLine) > 4 Then objTStream.WriteLine Mid(strThisLine, 6) Next objTStream.Close End If …
圖5-16 向文本文件追加內(nèi)容時(shí)的示例頁(yè)面 4. 重寫缺省內(nèi)容 最后,Restore按鈕用來將初始缺省內(nèi)容簡(jiǎn)單地重寫回文本文件。代碼與用TextStream的方法寫一個(gè)文本文件類似: … If Len(Request.Form("cmdDefault")) Then 'write out default contents to file Set objTStream = objFSO.CreateTextFile(strTextFile, True, False) objTStream.WriteLine "At last I can create files with VBScript!" objTStream.WriteLine objTStream.WriteLine "Here are three blank lines:" objTStream.WriteBlankLines 3 objTStream.Write "... and this is " objTStream.WriteLine "the last line." objTStream.Close End If