5.5.1 创建textstream对象的方法
有三个常用方法用于创建或打开一个文本文件并返回textstram对象,如表5-13所示:
表5-13 创建textstream对象的方法及说明
方 法
说 明
createtextfile
(filename,overwrite,unicode)
在磁盘上用指定的文件名filename创建一个新文本文件,并返回一个与该文件对应的textstream对象。如果可选的overwrite参数设置为true,将覆盖具有同样路径的同名文件。缺省的overwrite是false。如果可选的unicode参数设置为false,该文件的内容将存储为unicode格式。缺省的unicode是false
opentextfile
(filename,iomode,create,format)
打开或创建(如果不存在)一个名为filename的文件,并且返回与该文件对应的textstream对象。filename参数可以包含绝对或相对路径。iomode参数说明需要的访问类型。容许值是forreading(1)(缺省)、forwriting(2)、forappending(8)。写入或追加到一个不存在的文件时,如果create参数设置为true,将创建一个新文件。缺省的create是false。format参数说明了读或写文件时的数据格式。容许值是tristatefalse(0)(缺省),说明用ascii数据格式;tristatetrue(-1)说明用unicode数据格式;tristateusedefault(-2)说明数据使用系统缺省的格式
openastextstream
(iomode,format)
打开一个指定的文件并且返回一个textstream对象,可用于对该文件的读、写或追加。iomode参数说明了需要的访问类型。容许值是forreading(1)(缺省)、forwriting(2)、forappending(8)。format参数说明了读写文件的数据格式。容许值是tristatefalse(0)(缺省),说明用ascii数据格式;tristatetrue(-1)说明用unicode数据格式;tristateusedefault(-2)说明使用系统缺省的格式
上面列出的方法在filesystemobject、folder和file对象中的实现有所不同。如表5-14所示:
表5-14 三个对象中包含的方法
方 法
filesystemobject对象
folder对象
file对象
createtextfile
有
有
有
opentextfile
有
无
无
openastextstream
无
无
有
因此,可以使用这些方法创建一个新的文本文件,或者打开一个已存在的文件。则可得到与该文件相应的一个textstream对象,可以使用textstream对象的属性和方法操作文件。
1. 创建新的文本文件
可以用createtextfile方法创建新的文本文件,或覆盖一个已存在的文件。返回的textstream对象可用来读写文件。
首先创建一个filesystemobject对象,用来创建textstream对象。下面这个例子是用vbscript创建一个“普通的”(即非unicode)名为myfile.txt的文件,并覆盖已存在的同名文件:
set objfso = server.createobject("scripting.filesystemobject")
set objtstream = objfso.createtextfile("c:\textfiles\myfile.txt", true, false)
这同样可用jscript实现:
var objfso = server.createobject(scripting.filesystemobject);
var objtstream = objfso.createtextfile(c:\textfiles\myfile.txt, true, false);
一旦创建了该文件,就可以使用objtstream(它是对一个textstream对象的引用)对文件进行操作。
2. 打开已存在的文本文件
opentextfile方法用于打开一个已有的文本文件。它返回一个textstream对象,可用这个对象对文件读或追加数据。
同样,首先创建一个filesystemobject对象,然后用其创建一个textstream对象。下面的vbscript程序例子打开一个名为myfile.txt的文件,准备读出其内容:
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);
为了写入一个文件或创建一个不存在的文件,可以用以下代码:
in vbscript:
set objtstream = objfso.opentextfile("c:\textfiles\myfile.txt", forwriting, true)
// in jscript:
var objtstream = objfso.opentextfile(c:\textfiles\myfile.txt, forwriting, true);
如果要打开一个已有的unicode文件,准备对其追加数据,但是不创建不存在的文件,可以用:
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. 作为一个textstream对象打开一个file对象
可用file对象的openastextstream方法打开与该对象相应的文件,并且返回一个能对该文件进行读、写和追加的textstream对象。所以,给定一个file对象(这种情况下不是filesystemobject对象)——objfileobject,可作为一个“普通的”(非unicode)textstream对象打开它,以供追加文件内容:
in vbscript:
set objtstream = objfileobject.openastextstream(forappending, false)
// in jscript:
var objtstream = objfileobject.opentextfile(forappending, false);
注意,使用这种方法不需要文件名,因为程序的执行通过引用file对象进行,并且没有create参数,因为该文件必须已存在,如果想从一个新的空的文件开始,可以用:
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);
5.5.2 textstream对象成员概要
表5-15和表5-16是textstream对象的全部属性和方法的列表。下面将简短介绍各个重要的成员的细节。
1. textstream对象的属性
textstream的属性提供有关文件内文件指针当前位置的信息,如表5-15所示。注意,所有的属性是只读的。
表5-15 textstream对象的属性及说明
属 性
说 明
atendofline
如果文件位置指针在文件中一行的末尾则返回true
atendofstream
如果文件位置指针在文件的末尾则返回true
column
从1开始返回文件中当前字符的列号
line
从1开始返回文件中当前行的行号
atendofline和atendofstream属性仅对以iomode参数为forreading的方式打开的文件可用,否则将会出错。
2. textstream对象的方法
textstream对象的方法如表5-16所示:
表5-16 textstream对象的方法及说明
方 法
说 明
close()
关闭一个打开的文件
read(numchars)
从文件中读出numchars个字符
readall()
作为单个字符串读出整个文件
readline()
作为一个字符串从文件中读出一行(直到回车符和换行)
skip(numchars)
当从文件读出时忽略numchars个字符
skipline()
当从文件读出时忽略下一行
write(string)
向文件写入字符串string
writeline(string)
向文件写入字符串string(可选)和换行符
writeblanklines(n)
向文件写入n个换行符
3. 写文本文件
一旦使用createtextfile、opentextfile或openastextstream方法以及forwriting或forappending参数,创建一个对应于某个文件的textstream对象,可以用下面的vbscript程序写文件和关闭文件:
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参数,创建一个对应于某个文件的textstream对象,可以用下面的vbscript程序读文件和关闭文件:
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对象举例
为了了解使用textstream对象操作磨灭文件的几种方式,本书提供了一个vbscript示例页面,该页使用了大量的上述的代码。从示例的chapter05主菜单页,选择链接“working with the textstream object”打开show_textstream.asp页面。
该页显示了存储在磁盘上名为myfile.txt的文件的文本内容。在<textarea>控件里显示的文本内容允许进行编辑,并且下面有三个按钮。三个按钮的作用分别是用<textarea>控件的内容更新(即取代)最初的文本,在已有文件内容的后面添加文本,或用初始的缺省内容重写文件,如图5-15所示:
图5-15 textstream对象示例页面
1. 读取已存在的文本文件的内容
每次载入该页面,将打开文本文件并将读取的内容置入<textarea>控件。注意我们如何使用server.mappath方法得到文件myfile.txt的绝对物理路径,该文件与示例页面在同一目录下。下面的程序创建filesystemobject实例:
<%
strtextfile = server.mappath("myfile.txt")
create an instance of a filesytemobject object
set objfso = server.createobject("scripting.filesystemobject")
…
与书中本部分的大多数其他示例一样,该页包含一个<form>段以保存该页面的html控件。action是当前页面,因此窗体的内容送回到同一页面。
每次载入该页面,<textarea>控件用文本文件的当前内容填充:
…
<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>
由上面程序可知道如何打开文本文件进行读取,遍历整个文件每次读取一行(而不是作为一个字符串读出整个文件)。这是因为要添加自己的行号,行号不属于该文件的文本。对从该文件读出的每行(读之前),检索并且格式化line属性并创建一个三位数字的行号。然后把行编号和文本行放置页面的<textarea>控件内。
2. 更新文本文件的内容
当点击页面的update按钮时(一般是在编辑了<textarea>控件里的文本以后),将把<textarea>控件里的内容重新写入到该文本文件内。为此,该页有相应的一些asp代码,在创建html控件以前检验request.form集合,查看点击的是哪一个按钮(如果有的话),然后就重新载入该页。
如果点击update按钮,搜集<textarea>控件的内容作为一个字符串,分离这个字符串使之成为独立文本选择的数组,并且打开文本文件准备重写其内容,然后遍历刚刚创建的数组,按行号循环写入该行的内容:
…
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页内的内容格式和wrap属性的设置。特别是应在asp脚本结束定界符“%>”后立即写上</textarea>标记,以防止增加一个额外的回车符号。即使用:
%></textarea><p>
而不使用:
%>
</textarea><p>
3. 向文本文件追加内容
当点击append按钮时,可对已有的文件追加内容,与修改该文件内容类似,如图5-16所示。区别是打开该文件是为了追加而不是为了改写文件。调用opentextfile方法时可增加额外参数,防止在指定的文件2不存在时创建新文件。
…
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 向文本文件追加内容时的示例页面
4. 重写缺省内容
最后,restore按钮用来将初始缺省内容简单地重写回文本文件。代码与用textstream的方法写一个文本文件类似:
…
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
5.6 小结
本章讲述了在asp页面中使用对象和组件的强大能力。首先讨论对象和组件的一般特性,以及它们的类型。然后集中论述了如何在asp(及客户端)脚本代码内创建对象实例。
在页面上使用的许多对象可能都是“外部”组件,这些组件安装在服务器上,独立于asp。本章所讨论的对象,当asp使用一种缺省的脚本语言(如vbscript或jscript)时,总是可用的。其实现是通过scrrun.dll文件里的脚本运行期库完成的。
这些对象是指dictonary对象、filesystemobject对象和textstream对象。
dictionary对象为我们提供了存储值的一种有效方式,可根据名字进行索引和访问,而不是根据一个数字进行访问。这是存储名字/值对这样的数据的理想方式。
filesystemobject对象和textstream对象相互之间联系密切,可使用它们访问服务器或网络(映射)的磁盘驱动器的目录。filesystemobject对象提供对驱动器、文件夹(目录)和文件的存取,并提供了用于对于获得更多的信息或移动、复制、删除它们的属性及方法。
可以创建对应于系统内的任何文件的textstream对象,并通过该对象对文件进行读取和写入。对于读写过程它作为文本文件操作,甚至可以处理unicode格式的文件。对文件系统的导航和读写能力的结合允许对服务器文件系统进行极其复杂的控制。还可以在客户端脚本代码中使用对象(有一定限制)。
