ASP CreateTextFile() 方法
ASP CreateTextFile 方法用于在当前文件夹或目录中创建一个新的文本文件。它返回一个可用于对文件执行操作的 TextStream 对象。
句法:
对于文件夹对象:
FolderObject.CreateTextFile(filename, overwrite, unicode)
对于文件系统对象:
FileSystemObject.CreateTextFile(filename, overwrite, unicode)
参数:该属性具有上述三个参数,如下所述:
- filename:指定要新建的文本文件的名称。
- overwrite:它包含一个布尔值,指示是否可以覆盖现有文件。 true 值表示可以覆盖文件,false 表示不允许覆盖。默认值是true。它是一个可选参数。
- unicode:它包含一个布尔值,指示文件是创建为 Unicode 文件还是 ASCII 文件。 true 值表示文件创建为 Unicode 文件,false 值表示文件创建为 ASCII 文件。默认值为假。它是一个可选参数。
下面的示例演示了ASP CreateTextFile 方法。
示例 1:下面的示例演示了 ASP Folder.CreateTextFile 方法。
ASP
<%
dim fs,fo,tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'Getting the folder where the file has to be created
Set fo=fs.GetFolder("d:\GFG")
'Creating a new text file
Set tfile=fo.CreateTextFile("GFG.txt",false)
'Writing a line to the file
tfile.WriteLine("Hello Geeks!")
'Closing the file
tfile.Close
Response.write("A new textfile has been created!")
'Reading the new text file
Set tfile=fs.OpenTextFile("d:\GFG\GFG.txt", 1)
Response.Write("
The File Contents are: " & tfile.ReadAll)
tfile.Close
set tfile=nothing
set fo=nothing
set fs=nothing
%>
ASP
<%
dim fs,txtfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
'Creating the text file at the given path
set txtfile=fs.CreateTextFile("D:\gfg.txt")
'Writing a line to the file
txtfile.WriteLine("Hello World!")
'Closing the file
txtfile.Close
Response.Write("The new text file has been created!")
'Reading the new text file
Set txtfile=fs.OpenTextFile("d:\GFG.txt", 1)
Response.Write("
The File Contents are: " & txtfile.ReadAll)
txtfile.Close
set txtfile=nothing
set fs=nothing
%>
输出:
A new textfile has been created!
The File Contents are: Hello Geeks!
示例 2:下面的示例演示了 ASP FileSystemObject.CreateTextFile 方法。
ASP
<%
dim fs,txtfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
'Creating the text file at the given path
set txtfile=fs.CreateTextFile("D:\gfg.txt")
'Writing a line to the file
txtfile.WriteLine("Hello World!")
'Closing the file
txtfile.Close
Response.Write("The new text file has been created!")
'Reading the new text file
Set txtfile=fs.OpenTextFile("d:\GFG.txt", 1)
Response.Write("
The File Contents are: " & txtfile.ReadAll)
txtfile.Close
set txtfile=nothing
set fs=nothing
%>
输出:
The new text file has been created!
The File Contents are: Hello World!