Class

# TextInputStream

<div class="rst-class">

forsearch

</div>

TextInputStream

<div class="rst-class">

forsearch

</div>

Stream

<div class="rst-class">

forsearch

</div>

Textfile

<div class="rst-class">

forsearch

</div>

File

<div class="rst-class">

forsearch

</div>

Files

<div class="rst-class">

forsearch

</div>

Reading

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

In order to read text from a file, you need to create a <span class="title-ref">TextInputStream</span> object. <span class="title-ref">TextInputStreams</span> 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.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                         | Type                                                 | Read-Only | Shared |
|----------------------------------------------|------------------------------------------------------|-----------|--------|
| `BytePosition<textinputstream.byteposition>` | `UInt64</api/data_types/additional_types/uint64>`    |           |        |
| `Encoding<textinputstream.encoding>`         | `TextEncoding</api/text/encoding_text/textencoding>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                        | Parameters                                                                                                                               | Returns                                        | Shared |
|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|--------|
| `Close<textinputstream.close>`              |                                                                                                                                          |                                                |        |
| `Constructor<textinputstream.constructor0>` | handle As `Ptr</api/data_types/additional_types/ptr>`, type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`                        |                                                |        |
| `EndOfFile<textinputstream.endoffile>`      |                                                                                                                                          | `Boolean</api/data_types/boolean>`             |        |
| `Handle<textinputstream.handle_method>`     | type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`                                                                               | `Ptr</api/data_types/additional_types/ptr>`    |        |
| `Open<textinputstream.open>`                | file As `FolderItem</api/files/folderitem>`                                                                                              | <span class="title-ref">TextInputStream</span> | ✓      |
| `Read<textinputstream.read>`                | Count As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>` | `String</api/data_types/string>`               |        |
| `ReadAll<textinputstream.readall>`          | encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`                                              | `String</api/data_types/string>`               |        |
| `ReadError<textinputstream.readerror>`      |                                                                                                                                          | `Boolean</api/data_types/boolean>`             |        |
| `ReadLine<textinputstream.readline>`        | encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`                                              | `String</api/data_types/string>`               |        |

## Property descriptions

<div id="textinputstream.byteposition">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.BytePosition

**BytePosition** As `UInt64</api/data_types/additional_types/uint64>`

> Indicates the byte position of the file pointer, not the character position.

<div id="textinputstream.encoding">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Encoding

**Encoding** As `TextEncoding</api/text/encoding_text/textencoding>`

> Specifies the encoding to be defined for a string returned by ReadLine or ReadAll.
>
> It does not actually convert the bytes, but only assigns them an encoding, as if you had called `DefineEncoding</api/text/encoding_text/defineencoding>`. Use the `Encoding</api/text/encoding_text/encoding>` object to specify the `TextEncoding</api/text/encoding_text/textencoding>`. It defaults to UTF-8, but you can assign it a different encoding to match your file, or even assign `Nil</api/language/nil>` if you want the string to have an undefined encoding.
>
> This example sets the encoding for a file:
>
> ``` xojo
> Var f As FolderItem = SpecialFolder.Documents.Child("test.txt")
> Var t As TextInputStream = TextInputStream.Open(f)
> t.Encoding = Encodings.UTF8
> ```

## Method descriptions

<div id="textinputstream.close">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Close

**Close**

> Closes the file.

<div id="textinputstream.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Constructor

**Constructor**(handle As `Ptr</api/data_types/additional_types/ptr>`, type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Creates a <span class="title-ref">TextInputStream</span> instance.
>
> *type* is one of the `IOStreamHandleTypes<runtime.iostreamhandletypes>` and *handle* is the appropriate handle type specified by the *type* parameter.
>
> For instance, you can use a `Declare</api/language/declare>` to open a file with whatever permissions that you wish, and then pass the Handle to a stream object's constructor.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not supported for Android.
>
> </div>

<div id="textinputstream.endoffile">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.EndOfFile

**EndOfFile** As `Boolean</api/data_types/boolean>`

> Returns `True</api/language/true>` when there's no more data left to read.
>
> This code reads the rows and columns of data from a tab-delimited text file into a `ListBox</api/user_interface/desktop/desktoplistbox>`:
>
> ``` xojo
> Var f As FolderItem
> Var textInput As TextInputStream
> Var rowFromFile As String
>
> f = FolderItem.ShowOpenFileDialog("text/plain") ' defined as a FileType
> If f <> Nil Then
>   textInput = TextInputStream.Open(f)
>   textInput.Encoding = Encodings.UTF8
>
>   Do
>     rowFromFile = textInput.ReadLine
>     Var values() As String = rowFromFile.ToArray(String.Chr(9))
>     ListBox1.ColumnCount = values.Count
>     ListBox1.AddRow("")
>     Var col As Integer
>     For Each value As String In values
>       ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, col) = value
>       col = col + 1
>     Next
>   Loop Until textInput.EndOfFile
>
>   textInput.Close
> End If
> ```
>
> This example reads each pair of bytes from a file and writes them in reverse order to a new file. The user chooses the source file using the Open-file dialog box and saves the new file using the Save as dialog box. The EOF property is used to terminate the `Do...Loop</api/language/loops/do...loop>`.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   Var writeFile As FolderItem = FolderItem.ShowSaveFileDialog("", "")
>   If writeFile <> Nil Then
>     Var writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
>     writeStream.LittleEndian = True
>     Do Until ReadStream.EndOfFile
>       writeStream.WriteInt8(ReadStream.ReadInt8)
>     Loop
>     writeStream = Nil
>   End If
>   readStream = Nil
> End If
> ```

<div id="textinputstream.handle_method">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Handle

**Handle**(type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`) As `Ptr</api/data_types/additional_types/ptr>`

> Handle returns a handle of the *type* passed or -1 if the requested *type* cannot be retrieved.
>
> See `IOStreamHandleTypes<runtime.iostreamhandletypes>` for a list of valid types.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not supported for Android.
>
> </div>

<div id="textinputstream.open">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Open

**Open**(file As `FolderItem</api/files/folderitem>`) As <span class="title-ref">TextInputStream</span>

> Opens the passed *file* to be read as a text file. Returns a <span class="title-ref">TextInputStream</span>. An IO error will trigger an `IOException</api/exceptions/ioexception>`.
>
> This method is `shared</api/language/shared>`.
>
> Reading from the *file* will begin at the start of the file. If you wish to read from any other point in the file, use the `BytePosition<textinputstream.byteposition>` property to move the read position.
>
> This shared method replaces the deprecated `FolderItem</api/files/folderitem>`.OpenAsTextFile.
>
> ``` xojo
> Var f As FolderItem
> Var t As TextInputStream
> f = FolderItem.ShowOpenFileDialog("text") ' file type defined in File Type Sets Editor
> If f <> Nil Then
>   t = TextInputStream.Open(f)
>   t.Encoding = Encodings.UTF8 //specify encoding of input stream
>   TextArea1.Text = t.ReadAll
>   t.Close
> End if
> ```

<div id="textinputstream.read">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.Read

**Read**(Count As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Reads *Count* bytes from the input stream and returns a `String</api/data_types/string>`.
>
> If provided, the optional parameter *encoding* specifies the text encoding to be defined for the `String</api/data_types/string>` to be read.
>
> If *Count* is higher than the amount of bytes currently available in the stream, all available bytes will be returned. Therefore, make sure to always consider the case that you get less than you requested. To see if you received all requested bytes, check the returned string's `String<string.bytes>` property (avoid using `Length<binarystream.length>` as it may give a different number if the encoding is not `Nil</api/language/nil>`).
>
> If not enough memory is available, you get back an empty string.
>
> This example reads the first 1000 bytes from a `BinaryStream</api/files/binarystream>`.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   TextArea1.Text = ReadStream.Read(1000, Encodings.UTF8)
> End If
> ```

<div id="textinputstream.readall">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.ReadAll

**ReadAll**(encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Returns all of the text from the current position to the end of the file as a `String</api/data_types/string>`.
>
> The optional *encoding* parameter enables you to specify the encoding of the text. If you pass `Nil</api/language/nil>`, the default encoding is used. This is usually UTF-8, unless it was set to another encoding via an assignment statement. If you want to set the encoding to `Nil</api/language/nil>`, use the Encoding property instead.
>
> This example is opens a text file that the user selects into a <span class="title-ref">TextInputStream</span> and then displays it in a `TextArea</api/user_interface/desktop/desktoptextarea>`.
>
> ``` xojo
> Var f As FolderItem
> Var dlg As OpenFileDialog
> Var t As TextInputStream
>
> ' create a new OpenFileDialog
> dlg = New OpenFileDialog
> ' set what type of file it looks for
> dlg.Filter = "text/plain"
>
> ' display the dialog
> f = dlg.ShowModal
>
> ' check to make sure the user didn't click cancel
> If f <> Nil Then
>   t = TextInputStream.Open(f)
>   ' make sure we could open it
>   If t <> Nil Then
>     ' Read all of t into myTextArea.text
>     MyTextArea.Text = t.ReadAll
>     ' close the file so that other applications can use it
>     t.Close
>   Else
>     ' the file could not be a read as a text file
>     MessageBox("The selected file is not a text file.")
>   End If
> Else
>   ' the user clicked cancel... just ignore it
> End If
> ```

<div id="textinputstream.readerror">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.ReadError

**ReadError** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>` then an error occurred during reading.

<div id="textinputstream.readline">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

TextInputStream.ReadLine

**ReadLine**(encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Returns the next line of text (as a `string</api/data_types/string>`) from the <span class="title-ref">TextInputStream</span>. Any valid end-of-line indicator is used to identify a line.
>
> The optional *encoding* parameter enables you to specify the encoding of the text. If you pass `Nil</api/language/nil>`, the default encoding is used. This is usually UTF-8, unless it was set to another encoding via an assignment statement. If you want to set the encoding to `Nil</api/language/nil>`, use the Encoding property instead.
>
> This example reads the rows and columns of data from a tab-delimited text file into a `ListBox</api/user_interface/desktop/desktoplistbox>`:
>
> ``` xojo
> Const kTab As String = &u9
>
> Var f As FolderItem
> Var textInput As TextInputStream
> Var rowFromFile, oneCell As String
>
> f = FolderItem.ShowOpenFileDialog("text/plain") ' defined as a FileType
> If f <> Nil Then
>   textInput = TextInputStream.Open(f)
>   textInput.Encoding = Encodings.UTF8
>   Do
>     rowFromFile = textInput.ReadLine
>     If ListBox1.ColumnCount < rowFromFile.CountFields(kTab) Then
>       ListBox1.ColumnCount = rowFromFile.CountFields(kTab)
>     End If
>
>     ListBox1.AddRow(rowFromFile.NthField(kTab, 1))
>     For i As Integer =1 To rowFromFile.CountFields(kTab)
>       oneCell = rowFromFile.NthField(kTab, i)
>       ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, i - 1) = oneCell
>     Next
>   Loop Until textInput.EndOfFile
>   textInput.Close
> End If
> ```

## Interfaces

This class implements the `Readable</api/files/readable>` class interface.

## Notes

When reading a file, the default encoding is UTF8. If the file has no encoding, read it instead with a `BinaryStream</api/files/binarystream>`.

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</api/text/encoding_text/encodings>` module to get the encoding and use it to set the value of the Encoding property of the <span class="title-ref">TextInputStream</span> object. Here is an example that reads a text file that uses the MacRoman encoding:

``` xojo
Var f As FolderItem = FolderItem.ShowOpenFileDialog("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
    Var t As TextInputStream
    Try
      t = TextInputStream.Open(f)
      t.Encoding = Encodings.MacRoman
      TextArea1.Text = t.ReadAll
    Catch e As IOException
      MessageBox("Error accessing file.")
    End Try
    t.Close
  End If
End If
```

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

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

instead of

``` xojo
t.Encoding = Encodings.MacRoman
```

## Sample code

This example reads the rows and columns of data from a tab-delimited text file into a `DesktopListBox</api/user_interface/desktop/desktoplistbox>`:

``` xojo
Var f As FolderItem
Var textInput As TextInputStream
Var rowFromFile, oneCell As String
Var i As Integer
f = FolderItem.ShowOpenFileDialog("text/plain") ' defined as a FileType
If f <> Nil And f.Exists Then
  Var tab As String = String.ChrByte(9)
  textInput = TextInputStream.Open(f)
  While Not textInput.EndOfFile
    rowFromFile = textInput.ReadLine

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

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `ConvertEncoding</api/text/encoding_text/convertencoding>`, `DefineEncoding</api/text/encoding_text/defineencoding>`, `Encoding</api/text/encoding_text/encoding>` functions; `BinaryStream</api/files/binarystream>`, `IOException</api/exceptions/ioexception>`, `TextEncoding</api/text/encoding_text/textencoding>`, `TextOutputStream</api/files/textoutputstream>` classes; `Encodings</api/text/encoding_text/encodings>` module; `Readable</api/files/readable>` class interface.

</div>
