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
复制代码 这篇文章就结束到这了,需要的朋友可以参考一下。
来源:互联网
免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作! |
|