ScriptForge.TextStream service

The TextStream service is used to sequentially read from and write to files opened or created using the ScriptForge.FileSystem service.

The methods OpenTextFile and CreateTextFile from the FileSystem service return an instance of the TextStream service.

Line delimiters may be specified by the user. In input operations CR, LF or CR+LF are supported. In output operations, the default line delimiter is the one used by the operating system.

The line delimiter for the operating system where the macro is being executed can be accessed using the SF_String.sfNEWLINE property.

note

All operations needed to read from or write to a file (open, read/write and close) are presumed to happen during the same macro run.


Service instantiation

The code snippet below uses the OpenTextFile method to create an instance of the TextStream Service.


        GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
        Dim FSO As Variant
        FSO = CreateScriptService("FileSystem")
        Set myFile = FSO.OpenTextFile("C:\Temp\ThisFile.txt", FSO.ForReading)
    
note

Denne tenesta har full støtte både i Basic og Python. Alle eksempla er skrivne i programmeringsspråket Basic og kan enkelt omformast til Python.


The file must be closed with the CloseFile method after all read or write operations have been executed:


      myFile.CloseFile()
    

Optionally, the resources used by the TextStream instance can be released using the Dispose method:


      Set myFile = myFile.Dispose()
    
note

The methods in the TextStream service are mostly based on the XTextInputStream and XTextOutputStream UNO interfaces.


Properties

Name

Skriveverna

Type

Beskriving

AtEndOfStream

Yes

Boolean

Used in read mode. A True value indicates that the end of the file has been reached. A test using this property should precede calls to the ReadLine method.

Encoding

Yes

String

The character set to be used. The default encoding is "UTF-8".

FileName

Yes

String

Returns the name of the current file either in URL format or in the native operating system's format, depending on the current value of the FileNaming property of the FileSystem service.

IOMode

Yes

String

Indicates the input/output mode. Possible values are "READ", "WRITE" or "APPEND".

Line

Yes

Long

Returns the number of lines read or written so far.

NewLine

No

String

Sets or returns the current delimiter to be inserted between two successive written lines. The default value is the native line delimiter in the current operating system.


note

To learn more about the names of character sets, visit IANA's Character Set page. Beware that LibreOffice does not implement all existing character sets.


List of Methods in the TextStream Service

CloseFile
ReadAll

ReadLine
SkipLine

WriteBlankLines
WriteLine


CloseFile

Closes the current input or output stream and empties the output buffer if relevant. Returns True if the file was successfully closed.

Syntaks:


        myFile.CloseFile() As Boolean
    

ReadAll

Returns all the remaining lines in the text stream as a single string. Line breaks are not removed.

The resulting string can be split in lines either by using the Split built-in Basic function if the line delimiter is known, or with the SF_String.SplitLines method.

For large files, using the ReadAll method wastes memory resources. In such cases it is recommended to read the file line by line using the ReadLine method.

Syntaks:


        myFile.ReadAll() As String
    

Eksempel:

Consider the text file "Students.txt" with the following contents (a name in each line):


      Herbie Peggy
      Hardy Jarrett
      Edith Lorelle
      Roderick Rosamund
      Placid Everette
    

Eksemplet nedanfor brukar metodane ReadAll og SplitLines til å lesa innhaldet i fila inn i ei matrise med strengar:


      Sub ReadFile_Example
          ' Hentar tenesta FileSystem
          Dim FSO : FSO = CreateScriptService("FileSystem")
          ' Opnar tekstfila med dei namna som skal lesast
          Dim inputFile as Object
          Set inputFile = FSO.OpenTextFile("~/Documents/Students.txt")
          'Reads all the contents in the input file as a single string
          Dim allData as String
          allData = inputFile.ReadAll()
          'Splits the string into an array
          Dim arrNames as Variant
          arrNames = SF_String.SplitLines(allData)
          ' (...)
          inputFile.CloseFile()
      End Sub
    

ReadLine

Returnerer den neste linja i tekststraumen som ein streng. Linjeskift vert fjerna frå den returnerte strengen.

Testen AtEndOfStream må gjerast før metoden ReadLine slik som i eksempelet nedanfor.

Det vert sett opp ein feil viss AtEndOfStream vart nådd under det førre oppkallet av metodane ReadLine eller SkipLine.

Syntaks:


        myFile.ReadLine() As String
    

Eksempel:


        Dim sLine As String
        Do While Not myFile.AtEndOfStream
            sLine = myFile.ReadLine()
            ' (...)
        Loop
    

SkipLine

Hoppar over den neste linja i inndata-straumen ved lesing av einTextStream.

Denne metoden kan resultera i at AtEndOfStream vert sett til Sann.

Syntaks:


        myFile.SkipLine()
    

WriteBlankLines

Skriv eit spesifisert tal på tomme linjer til utdata-straumen.

Syntaks:


        myFile.WriteBlankLines(Lines As Long)
    

Parametrar:

Lines: Talet på kor mange tomme linjer som skal skrivast.

WriteLine

Skriv den gjevne strengen til utdata-straumen som ei enkelt linje.

Teiknet som er definert i eigenskapen NewLine vert brukt som linjeskiljeteikn.

Syntaks:


        myFile.WriteLine(Line As String)
    

Parametrar:

Line: Linja som skal skrivast kan vera tom.

Eksempel:


      Sub SquaredValuesFile(lastValue as Integer)
          ' Byrjar tenesta FileSystem
          Dim FSO as Variant : FSO = CreateScriptService("FileSystem")
          ' Lagar ei tekstfil
          Dim myFile as Variant : myFile = FSO.CreateTextFile("~/Documents/squares.csv")
          ' Skriv verdien og kvadratet av verdien skilde med «;»
          Dim value as Integer
          myFile.WriteLine("Value;Value Squared")
          For value = 1 To lastValue
              myFile.WriteLine(value & ";" & value ^ 2)
          Next value
          ' Lukkar fila og frigjev ressursar
          myFile.CloseFile()
          myFile.Dispose()
      End Sub
    
warning

Alle Basic-rutinane og -identifikatorane i ScriptForge som vert innleidde med understrek «_» er reserverte for internt bruk. Dei er ikkje tenkt brukte i Basic-makroar.