zoukankan      html  css  js  c++  java
  • VBS基础篇

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

     

    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 个换行符

     

     

      CloseWrite、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: esting.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

       ReadReadAll及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: est.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

        SkipSkipLine的使用

    方法名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: est.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 开始返回文件中当前行的行号

     

      AtEndOfLineAtEndOfStream的使用

    两者间的区别是:

    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: est.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

       ColumnLine的使用

    示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Call TestTextStream("c: est.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   

       文本读取示例:

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

    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: est.txt",1)
    '直到到达文件尾
    Do Until MyFile.AtEndOfStream
        '读取当前整行数据
        strLine = MyFile.ReadLine
    Loop
    MyFile.Close
    MsgBox strLine

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

    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: est.txt",1)
    Do Until MyFile.AtEndOfStream
        '读取当前整行字符串
        strNextLine = MyFile.ReadLine
        '判断读取的整行字符串是不是空白
        If Len(strNextLine) > 0 Then
            '不是空白,则赋值
            strLine = strNextLine
        End If
    Loop
    MyFile.Close
    MsgBox strLine

      读取文本指定行内容 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    MsgBox TestTextStream("c: est.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
  • 相关阅读:
    基于nginx结合openssl实现https
    更新续约与重新登陆
    DNS服务器
    ELK日志分析系统。
    OpenSSH远程控制
    DHCP配置
    DHCP服务概述
    网络服务
    磁盘配额
    磁盘配额
  • 原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/3834659.html
Copyright © 2011-2022 走看看