您的位置:首页 > 编程语言 > VB

VBS基础篇 - 对象(7) - TextStream对象

2014-08-26 15:21 483 查看
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(可选)和换行符。

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Dim
strPath,strText

strPath=
"C:\testing.txt"

strText=
"ThisisTest!helloword!"

'调用函数

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.WriteBlankLines3

'关闭TextStream对象

objStream.Close

End
Sub


Read、ReadAll及ReadLine的使用

方法名:Read(numchars)说明:从TextStream文件中读入指定数目的字符并返回结果字符串。

方法名:ReadAll()说明:读入全部TextStream文件并返回结果字符串。

方法名:ReadLine()说明:从TextStream文件中读入一整行字符(直到下一行,但不包括下一行字符),并返回字符串

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Call
CreateFile(
"c:\test.txt"
,

"ThisisTest!"
&vbCrLf&
"helloword!"
)


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文件时,跳过下一行。

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
"ThisisTest!"

&vbCrLf&
"helloword!"

'以只读的方式打开文件

Set
objStream=objFso.OpenTextFile(strPath,1,true)

'读取文件时跳过5个字符;或者跳过当前行,读取下一行

objStream.Skip(5)

MsgboxobjStream.ReadAll

Set
objStream=objFso.OpenTextFile(strPath,1,true)

'跳过第一行

objStream.SkipLine

MsgboxobjStream.ReadAll

'关闭TextStream对象

objStream.Close

End
Sub


  TextStream对象的属性

属性
说明
AtEndOfLine

如果文件位置指针在文件中一行的末尾则返回True

AtEndOfStream

如果文件位置指针在文件的末尾则返回True

Column

从1开始返回文件中当前字符的列号

Line

从1开始返回文件中当前行的行号”

  AtEndOfLine及AtEndOfStream的使用

两者间的区别是:

AtEndOfLine——读取到当前文本行的末尾;

AtEndOfStream——读取到整个文本的末尾

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

msgboxstr

str=
""

Set
objStream=objFso.OpenTextFile(strPath,1,true)

'如果当前的指针不在文本末端,则读取文本内容

Do
While
objStream.AtEndOfStream<>true

str=str+objStream.Read(1)

Loop

MsgBoxstr

'关闭TextStream对象

objStream.Close

End
Sub


  Column及Line的使用

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

'打印信息

MsgBoxstr

End
Sub


  文本读取示例:

  如何读取文本最后一行数据?  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

MsgBoxstrLine


  如何读取文本最后一行数据(文件末尾有空行)?  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

MsgBoxstrLine


 读取文本指定行内容 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MsgBoxTestTextStream(
"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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: