ReadLine 方法

TextStream 文件中读取整个行(直到但不包含换行符),并返回生成的字符串。

语法

object.ReadLine( )

参数

Object必需。始终为 TextStream 对象的名称。

注释

以下示例说明了 ReadLine 方法的用法:

示例 1:

Function ReadFirstLine(filepath)
    Const ForReading = 1
    Dim fso, theFile, firstLine
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set theFile = fso.OpenTextFile(filepath, ForReading)
    firstLine = theFile.ReadLine
    theFile.Close
    ReadFirstLine = firstLine
End Function

' Usage
Dim firstLineContent
firstLineContent = ReadFirstLine("C:\Path\To\Your\File.txt")

示例 2:

Function ReadFileLineByLine(filepath)
    Const ForReading = 1
    Dim fso, theFile, line
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set theFile = fso.OpenTextFile(filepath, ForReading)
    Do While Not theFile.AtEndOfStream
        line = theFile.ReadLine
     Loop
    theFile.Close
End Function

' Usage
ReadFileLineByLine "C:\Path\To\Your\File.txt"