TextInputStream

From Real Software 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.

Notes

On Windows and Macintosh only, TextInputStream objects can work with files larger than 2 gigabytes.


Properties
Encoding LastErrorCode
Handle PositionB


Methods
Close ReadAll
Read 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") //file type defined in File Type Sets Editor
If f <> Nil then
//Beware that TextInputStream.Open raises an exception if if fails.
Dim t As TextInputStream = TextInputStream.Open(f)
Try
t.Encoding = Encodings.MacRoman
TextArea1.text=t.ReadAll
Finally
t.Close
End Try
End if


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

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


instead of

t.Encoding=Encodings.MacRoman

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 Then
textInput =TextInputStream.Open(f)
textInput.Encoding=Encodings.MacRoman //strings are MacRoman
Do
rowFromFile=textInput.ReadLine
If ListBox1.ColumnCount < CountFields(rowFromFile,Chr(9)) Then
ListBox1.ColumnCount=CountFields(rowFromFile,Chr(9))
End If
ListBox1.AddRow NthField(rowFromFile,Chr(9),1)
For i=1 to CountFields(rowFromFile,Chr(9))
oneCell=NthField(rowFromFile,Chr(9),i)
ListBox1.Cell(ListBox1.ListCount-1,i-1)=oneCell
Next
Loop Until textInput.EOF
textInput.Close
End If


See Also

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

Personal tools