Class

Xojo.IO.TextInputStream


Warning

This item was deprecated in version 2020r2. Please use TextInputStream as a replacement.

Description

Used to read text of a specific encoding from a file.

Properties

Name

Type

Read-Only

Shared

BytePosition

UInt64

Encoding

TextEncoding

Methods

Name

Parameters

Returns

Shared

Close

Constructor

handle As Ptr, type As Xojo.IO.HandleTypes, encoding As TextEncoding

EOF

Boolean

Handle

type As Xojo.IO.HandleTypes

Ptr

Open

file As FolderItem, encoding As TextEncoding

TextInputStream

Read

numberOfBytes As UInt64

String

ReadAll

String

ReadLine

String

Property descriptions


Xojo.IO.TextInputStream.BytePosition

BytePosition As UInt64

Gets or sets the byte position of the file pointer, not the character position. The first position is numbered zero.


Xojo.IO.TextInputStream.Encoding

Encoding As TextEncoding

Gets or sets the default encoding for the Read, ReadAll and ReadLine methods.

Using Xojo.Core
inputStream.Encoding = TextEncoding.ASCII

Method descriptions


Xojo.IO.TextInputStream.Close

Close

Closes the stream. The stream is also closed automatically when it goes out of scope.

inputStream.Close

Xojo.IO.TextInputStream.Constructor

Constructor(handle As Ptr, type As Xojo.IO.HandleTypes, encoding As TextEncoding)

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

Creates a new TextInputStream based on the handle that was passed.


Xojo.IO.TextInputStream.EOF

EOF As Boolean

Returns True when a Read or ReadLine method reaches the end of the text stream.

Var line As Text
While Not inputStream.EOF
  line = inputStream.ReadLine
Wend

Xojo.IO.TextInputStream.Handle

Handle(type As Xojo.IO.HandleTypes) As Ptr

Returns a handle to the stream for use by OS API calls.


Xojo.IO.TextInputStream.Open

Open(file As FolderItem, encoding As TextEncoding) As TextInputStream

Creates a TextInputStream and connects it to the specified file.

Open a file and read all its text:

Using Xojo.Core
Using Xojo.IO

Var f As FolderItem
f = SpecialFolder.Documents.Child("SaveData.txt")

If Not f.Exists Then
  ' Cannot read the file if it does not exist
  Return
End If

Var errorText As Text
Try
  Var input As TextInputStream
  input = TextInputStream.Open(f, TextEncoding.UTF8)
  TextFileArea.Value = input.ReadAll
  input.Close
Catch e As IOException
  errorText = "File IO Error: " + e.Reason
End Try

Xojo.IO.TextInputStream.Read

Read(numberOfBytes As UInt64) As String

Reads the specified number of bytes from the stream and returns them as a Text object.


Xojo.IO.TextInputStream.ReadAll

ReadAll As String

Reads all remaining bytes from the stream and returns them as a Text object.

Var allText As Text
allText = inputStream.ReadAll

Xojo.IO.TextInputStream.ReadLine

ReadLine As String

Reads bytes from the stream until an EOL character is encountered and returns them as a Text object.

Load each line of the text file into an array:

Using Xojo.Core
Using Xojo.IO

Var f As FolderItem
f = SpecialFolder.Documents.Child("SaveData.txt")

If Not f.Exists Then
  ' Cannot read the file if it does not exist
  Return
End If

Var errorText As Text
Var lines() As Text
Try
  Var input As TextInputStream
  input = TextInputStream.Open(f, TextEncoding.UTF8)
  While Not input.EOF
    lines.AddRow(input.ReadLine)
  Wend
  input.Close
Catch e As IOException
  errorText = "File IO Error: " + e.Reason
End Try

Compatibility

All project types on all supported operating systems.

See also

Object parent class; BinaryStream, FolderItem, TextOutputStream classes