當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


ASP OpenAsTextStream用法及代碼示例

ASP OpenAsTextStream方法用於通過打開指定的文件來返回TextStream對象。它可用於對文件執行某些操作。

用法:

FileObject.OpenAsTextStream(mode, format)

參數:此方法具有上述和以下所述的兩個參數:

  • mode:它指定了打開文件的模式。它包含3個常量值,用於對文件執行操作。
    • 閱讀(1):這將打開文件以供讀取。無法在此模式下寫入文件。
    • 撰寫(2):這將打開文件進行寫入。
    • ForAppending(8):這將打開用於將內容附加到末尾的文件。
  • format:可選屬性。它包含三個用於定義文件格式的常量值。
    • TristateFalse(0):這將以ASCII格式打開文件。這是默認格式。
    • TristateTrue(1):這將以Unicode格式打開文件。
    • TristateUseDefault(2):這將使用係統默認值打開文件。

例:下麵的代碼演示了ASP File.OpenAsTextStream方法。

ASP


<%
dim fs,f,ts
set fs=Server.CreateObject("Scripting.FileSystemObject")
  
'Getting the file to be read
Set f=fs.GetFile("d:\GFG.txt")
  
'Opening the file as a text stream
'in writing mode (2)
Set ts=f.OpenAsTextStream(2)
  
'Writing some content to the file'
ts.Write("This is a GeeksforGeeks example")
  
'Closing the stream
ts.Close
  
'Opening the file as a text stream
'in reading mode (1)
Set ts=f.OpenAsTextStream(1)
  
'Reading the contents of the file
Response.Write(ts.ReadAll)
  
'Closing the stream
ts.Close
  
set ts=nothing
set f=nothing
set fs=nothing
%>

輸出:

This is a GeeksforGeeks example

相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 ASP OpenAsTextStream Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。