textstream对象是用于访问文本文件的对象,它是filesystemobject一个独立的附属对象,但在使用textstream对象时,我们仍要借助filesystemobject 对象或其附属对象来创建一个 textstream 对象并访问磁盘文件的内容。可以通过filesystemobject 对象的createtextfile()及opentextfile(),来获取textstream的对象句柄。
下面我们来具体的看看textstream 对象的方法及属性的使用
textstream对象的方法
| 方法 | 说明 |
| close() | 关闭一个打开的文件 |
| read(numchars) | 从文件中读出 numchars 个字符 |
| readall() | 作为单个字符串读出整个文件 |
| readline() | 作为一个字符串从文件中读出一行(直到回车符和换行) |
| skip(numchars) | 当从文件读出时忽略 numchars 个字符 |
| skipline() | 当从文件读出时忽略下一行 |
| write(string) | 向文件写入字符串 string |
| writeline(string) | 向文件写入字符串 string(可选)和换行符 |
| writeblanklines(n) | 向文件写入 n 个换行符 |
close、write、writeline及writeblanklines的使用
方法名:close()
说明:关闭正在打开的文件
方法名:writeline(string)
说明:向文件写入字符串 string(可选)和换行符。
示例:
dim strpath,strtext
strpath = "c:\testing.txt"
strtext = "this is test !hello word !"
'调用函数
call createfile(strpath,strtext)
sub createfile(strpath,strtext)
dim objfso,objstream
'创建filesystemobject对象
set objfso = createobject("scripting.filesystemobject")
'使用createtextfile(),来返回一个textstream对象句柄
set objstream = objfso.createtextfile(strpath,true)
'三个write的意思为:在文本中写入字符、写入带换行符的字符、写入3个换行符
objstream.write(strtext)
objstream.writeline(strtext)
objstream. writeblanklines 3
'关闭textstream对象
objstream.close
end sub
read、readall及readline的使用
方法名:read(numchars)
说明:从 textstream文件中读入指定数目的字符并返回结果字符串。
方法名:readall()
说明:读入全部 textstream文件并返回结果字符串。
方法名:readline()
说明:从 textstream文件中读入一整行字符(直到下一行,但不包括下一行字符),并返回字符串
示例:
call createfile("c:\test.txt", "this is test !" & vbcrlf & "hello word !")
sub createfile(strpath,strtext)
dim objfso,objstream
'创建filesystemobject对象
set objfso = createobject("scripting.filesystemobject")
'使用filesystemobject对象的createtextfile(),来返回一个textstream对象句柄
set objstream = objfso.createtextfile(strpath,true)
'写入字符
objstream.writeline(strtext)
'读取字符串分别是:读取整行、读取所有、读取指定数目的字符
msgbox (objstream.readline)
set objstream = objfso.opentextfile(strpath,1,true)
msgbox (objstream.readall)
set objstream = objfso.opentextfile(strpath,1,true)
msgbox (objstream.read(9))
'关闭textstream对象
objstream.close
end sub
skip、skipline的使用
方法名:skip(numchars)
说明:读取 textstream文件时跳过指定数目的字符
方法名:skipline()
说明:当读到 textstream文件时,跳过下一行。
示例:
dim strpath,strtext
strpath = "c:\test.txt"
'调用函数
call createfile(strpath)
sub createfile(strpath)
dim objfso,objstream
'创建filesystemobject对象
set objfso = createobject ("scripting.filesystemobject")
'使用filesystemobject对象的createtextfile(),来返回一个textstream对象句柄
set objstream = objfso.createtextfile(strpath,true)
'在文本中写入字符
objstream.write "this is test !" & vbcrlf & "hello word !"
'以只读的方式打开文件
set objstream = objfso.opentextfile(strpath,1,true)
'读取文件时跳过5个字符;或者跳过当前行,读取下一行
objstream.skip(5)
msgbox objstream.readall
set objstream = objfso.opentextfile(strpath,1,true)
'跳过第一行
objstream.skipline
msgbox objstream.readall
'关闭textstream对象
objstream.close
end sub
textstream对象的属性
|
属性 |
说明 |
|
atendofline |
如果文件位置指针在文件中一行的末尾则返回 true |
|
atendofstream |
如果文件位置指针在文件的末尾则返回 true |
|
column |
从 1 开始返回文件中当前字符的列号 |
|
line |
从 1 开始返回文件中当前行的行号” |
atendofline及atendofstream的使用
两者间的区别是:
atendofline——读取到当前文本行的末尾;
atendofstream——读取到整个文本的末尾
示例:
dim strpath,strtext
strpath = "c:\test.txt"
'调用函数
call createfile(strpath)
sub createfile(strpath)
dim objfso,objstream,str
'创建filesystemobject对象
set objfso = createobject ("scripting.filesystemobject")
'以只读的方式打开文件,如果文件不存在则创建它
set objstream = objfso.opentextfile(strpath,1,true)
'如果当前的指针不在行末,则读取文本内容
do while objstream.atendofline <> true
str = str + objstream.read(1)
loop
msgbox str
str = ""
set objstream = objfso.opentextfile(strpath,1,true)
'如果当前的指针不在文本末端,则读取文本内容
do while objstream.atendofstream <> true
str = str + objstream.read(1)
loop
msgbox str
'关闭textstream对象
objstream.close
end sub
column及line的使用
示例:
call testtextstream("c:\test.txt")
sub testtextstream(strpath)
dim objfso,objtstream,str
set objfso = createobject("scripting.filesystemobject")
'以只读的方式打开文件
set objtstream = objfso.opentextfile(strpath,1)
'如果当前的指针不在整个文档的末尾,读取文本的所有内容
do while objtstream.atendofstream <> true
objtstream.readall
str = str + "共有" & objtstream.line & "行数据,光标最后所在列号为:" &objtstream.column & vbcrlf
loop
'打印信息
msgbox str
end sub
文本读取示例:
如何读取文本最后一行数据?
dim fso,myfile
dim strline
'创建filesystemobject对象
set fso = createobject("scripting.filesystemobject")
'以只读的方式打开文件
set myfile = fso.opentextfile("c:\test.txt",1)
'直到到达文件尾
do until myfile.atendofstream
'读取当前整行数据
strline = myfile.readline
loop
myfile.close
msgbox strline
如何读取文本最后一行数据(文件末尾有空行)?
dim fso,myfile
dim strline
'创建filesystemobject对象
set fso = createobject("scripting.filesystemobject")
'以只读的方式打开文件
set myfile = fso.opentextfile("c:\test.txt",1)
do until myfile.atendofstream
'读取当前整行字符串
strnextline = myfile.readline
'判断读取的整行字符串是不是空白
if len(strnextline) > 0 then
'不是空白,则赋值
strline = strnextline
end if
loop
myfile.close
msgbox strline
读取文本指定行内容
msgbox testtextstream("c:\test.txt",1)
function testtextstream(strpath,intline)
dim fso,myfile
set fso = createobject("scripting.filesystemobject")
'以只读的方式打开文件
set myfile = fso.opentextfile(strpath,1)
'如果当前的指针不在整个文档的末尾,读取文本的整行内容
do until myfile.atendofstream
testtextstream = myfile.readline
intline = intline - 1
'判断光标是否已达到指定行,达到则退出函数
if intline = 0 then
exit function
end if
loop
end function
这篇文章就结束到这了,需要的朋友可以参考一下。
发表评论