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

Zerbitzu hau erabat onartuta dago bai Basic bai Python lengoaietan. Adibide guztiak Basic programazio-lengoaian eman dira, baina erraz bihurtu daitezke Python lengoaiara.


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.


Propietateak

Izena

Irakurtzeko soilik

Mota

Deskribapena

AtEndOfStream

Bai

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

Bai

String

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

FileName

Bai

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

Bai

String

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

Line

Bai

Long

Returns the number of lines read or written so far.

NewLine

Ez

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

Karaktere-jokoen izenei buruz gehiago jakiteko, begiratu IANAk karaktere-jokoei buruz duen orria. Kontuan izan LibreOffice aplikazioak ez dituela dauden karaktere-joko guztiak inplementatu.


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.

Sintaxia:


        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.

Fitxategi handien kasuan, ReadAll metodoa erabiltzen bada memoria-baliabideak alferrik galtzen dira. Kasu horietan, hobe da fitxategia lerroz lerro irakurtzea ReadLine metodoaren bidez.

Sintaxia:


        myFile.ReadAll() As String
    

Adibidea:

Adibidez, demagun "Students.txt" fitxategiak honako edukiak dituela (izen bana lerro bakoitzean):


      Herbie Peggy
      Hardy Jarrett
      Edith Lorelle
      Roderick Rosamund
      Placid Everette
    

Beheko adibideak ReadAll eta SplitLines metodoak erabiltzen ditu fitxategi baten edukiak irakurri eta kateen matrize batean jartzeko:


      Sub ReadFile_Example
          ' FileSystem zerbitzua kargatzen du
          Dim FSO : FSO = CreateScriptService("FileSystem")
          ' Irakurri behar diren izenak dituen testu-fitxategia irekitzen du
          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

Hurrengo lerroa itzultzen du testu-korrontea kate bat bada. Lerro-jauziak ezabatu egingo dira itzulitako fitxategitik.

AtEndOfStream probak ReadLine metodoaren aurretik egon behar du, beheko adibidean bezala.

Errore bat sortuko da AtEndOfStream iristen bada aurreko ReadLine edo SkipLine metodoari deitu baino lehen.

Sintaxia:


        myFile.ReadLine() As String
    

Adibidea:


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

SkipLine

Sarrera-korronteko hurrengo lerroa saltatzen du TextStream fitxategi bat irakurtzean.

Metodo honen emaitza izan daiteke AtEndOfStream propietatean True ezartzea.

Sintaxia:


        myFile.SkipLine()
    

WriteBlankLines

Lerro hutsen kopuru zehatz bat idazten du irteera-korrontean.

Sintaxia:


        myFile.WriteBlankLines(Lines As Long)
    

Parametroak:

Lines: Idatziko den lerro hutsen kopurua.

WriteLine

Emandako katea irteera-korrontean idazten du lerro bakar gisa.

NewLine propietatean definitutako karakterea lerro-mugatzaile gisa erabiliko da.

Sintaxia:


        myFile.WriteLine(Line As String)
    

Parametroak:

Line: Idatziko den lerroa, hutsik egon daiteke.

Adibidea:


      Sub SquaredValuesFile(lastValue as Integer)
          'FileSystem zerbitzuaren instantzia bat sortzen du
          Dim FSO as Variant : FSO = CreateScriptService("FileSystem")
          'Testu-fitxategi bat sortzen du
          Dim myFile as Variant : myFile = FSO.CreateTextFile("~/Documents/squares.csv")
          'Writes the Value and Value squared, separated by ";"
          Dim value as Integer
          myFile.WriteLine("Value;Value Squared")
          For value = 1 To lastValue
              myFile.WriteLine(value & ";" & value ^ 2)
          Next value
          'Fitxategia ixten du eta baliabideak askatzen ditu
          myFile.CloseFile()
          myFile.Dispose()
      End Sub
    
warning

Aurrizki gisa azpimarra bat ("_") duten ScriptForge Basic errutina edo identifikatzaile guztiak barneko erabilerarako erreserbatuta daude. Ez daude Basic makroetan erabiltzeko pentsatuta.