TextInputStream

From Xojo Documentation
Jump to: navigation, search

Class (inherits from Object)

In order to read text from a file, you need to create a TextInputStream object. TextInputStreams have methods that allow to read from a file, check to see if you are at the end of the file, and close the file when you are done reading from it. They are created by calling the Open shared method. If you are working with a file with an encoding that is not UTF-8, you should set the value of the Encoding property.

Properties
Encoding LastErrorCode ReadOnlyProperty.png
Handle ReadOnlyProperty.png PositionB


Methods
Close Read ReadError
EOF ReadAll ReadLine


Shared Methods
Open


Constructors

TextInputStream.Constructor(handle as Integer, type as Integer)


Class Constants

The following class constants can be used to specify the optional type parameter in the constructor and the Handle property.

Class Constant Description
HandleTypeWin32Handle A Windows 32 OS handle.
HandleTypeFilePointer A file pointer.
HandleTypeFileNumber A file descriptor.
HandleTypeMacFileRefNum A file reference number
HandleTypeMacFileSpecPointer An FSSpec.

Interfaces

The TextInputStream class implements the Readable class interface. Here are the methods with the parameters as specified by this class interface.

Name Parameters Return Type Description
EOF Boolean Returns True when the read reaches the end of the stream.
Read Count as Integer
[,enc as TextEncoding]
String Reads Count bytes from the input stream and returns a String.

If provided, the optional parameter Enc specifies the text encoding to be defined for the String to be read.

ReadError Boolean Returns True if an error of any type occurred.

Notes

When you read a text file that is from another operating system or in another language (or a mixture of languages) you may need to specify the text encoding that was used when the file was written. If you know the encoding, use the Encodings module to get the encoding and use it to set the value of the Encoding property of the TextInputStream object. Here is an example that reads a text file that uses the MacRoman encoding:

Dim f As FolderItem = GetOpenFolderItem("text") // as defined in File Type Sets Editor
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
TextArea1.Text = t.ReadAll
Catch e As IOException
t.Close
MsgBox("Error accessing file.")
End Try
End If
End If

To specify the encoding, you could instead use optional parameter of the ReadAll method:

TextArea1.Text = t.ReadAll(Encodings.UTF8)

instead of

t.Encoding = Encodings.UTF8

Examples

This example reads the rows and columns of data from a tab-delimited text file into a ListBox:

Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = GetOpenFolderItem("text/plain") // defined as a FileType
If f <> Nil And f.Exists Then
Dim tab As String = ChrB(9)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman // strings are MacRoman
While Not textInput.EOF
rowFromFile = textInput.ReadLine

// Set
If ListBox1.ColumnCount < CountFields(rowFromFile, tab) Then
ListBox1.ColumnCount = CountFields(rowFromFile, tab)
End If

ListBox1.AddRow("")
For i = 1 To CountFields(rowFromFile, tab)
oneCell = NthField(rowFromFile, tab, i)
ListBox1.Cell(ListBox1.ListCount - 1, i - 1) = oneCell
Next
Wend
textInput.Close
End If

See Also

ConvertEncoding, DefineEncoding, Encoding functions; BinaryStream, IOException, TextEncoding, TextOutputStream classes; Encodings module; Readable class interface.

Personal tools
Namespaces

Variants
Actions
Main
Content
Starting Out
Dig Deeper
More Help
Toolbox
About