Difference between revisions of "TextOutputStream"
From Xojo Documentation
(→Setting the Text Encoding) |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 16: | Line 16: | ||
{{Method | name=Close | description=Close()
Closes the TextOutputStream. }} | {{Method | name=Close | description=Close()
Closes the TextOutputStream. }} | ||
{{Method | name=Handle | link=Handle_Method | params=type As [[IOStreamHandleTypes]] | returntype=[[Ptr]] | description=Handle(type As IOStreamHandleTypes) As Ptr
Handle returns a handle of the Type passed or -1 if the requested Type cannot be retrieved. }} | {{Method | name=Handle | link=Handle_Method | params=type As [[IOStreamHandleTypes]] | returntype=[[Ptr]] | description=Handle(type As IOStreamHandleTypes) As Ptr
Handle returns a handle of the Type passed or -1 if the requested Type cannot be retrieved. }} | ||
− | {{Method | name=WriteLine | params= | + | {{Method | name=WriteLine | params=data as [[String]] | description=WriteLine(data as String)
Writes the text passed to the TextOutputStream and appends the Delimiter to the end of the line. }} |
</dynamicTable> | </dynamicTable> | ||
Line 25: | Line 25: | ||
<dynamicTable id="Constructors" class="methodTable" title="Constructors" columns="1"> | <dynamicTable id="Constructors" class="methodTable" title="Constructors" columns="1"> | ||
− | {{Ctor | params=handle as Ptr, type as IOStreamHandleTypes | description=TextOutputStream( | + | {{Ctor | params=handle as Ptr, type as IOStreamHandleTypes | description=TextOutputStream(handle as Ptr, type as IOStreamHandleTypes)
Creates a TextOutputStream instance using the passed handle and type. }} |
</dynamicTable> | </dynamicTable> | ||
Line 33: | Line 33: | ||
==Notes== | ==Notes== | ||
===Setting the Text Encoding=== | ===Setting the Text Encoding=== | ||
− | The default encoding is UTF8. If you need to write a file using a different encoding, use the [[TextOutputStream.Encoding|Encoding]] property before passing the text to the Write or WriteLine methods. | + | The default encoding is UTF8. If you want no encoding, use a [[BinaryStream]]. If you need to write a file using a different encoding, use the [[TextOutputStream.Encoding|Encoding]] property before passing the text to the Write or WriteLine methods. |
<rbcode> | <rbcode> | ||
Var documents As FolderItem = SpecialFolder.Documents | Var documents As FolderItem = SpecialFolder.Documents | ||
Line 43: | Line 43: | ||
Var output As TextOutputStream = TextOutputStream.Create(file) | Var output As TextOutputStream = TextOutputStream.Create(file) | ||
output.Encoding = Encodings.WindowsANSI | output.Encoding = Encodings.WindowsANSI | ||
− | output.Write(TextField1. | + | output.Write(TextField1.Text) |
output.Close | output.Close | ||
Catch e As IOException | Catch e As IOException | ||
Line 55: | Line 55: | ||
===Creating and Appending to a Text File=== | ===Creating and Appending to a Text File=== | ||
− | Use [[TextOutputStream.Open|Open]] when you want to open an existing text file and add text data to it. Use [[TextOutputStream.Create|Create]] to write to a new text file. The following two examples illustrate the difference. Each example writes the text in [[ | + | Use [[TextOutputStream.Open|Open]] when you want to open an existing text file and add text data to it. Use [[TextOutputStream.Create|Create]] to write to a new text file. The following two examples illustrate the difference. Each example writes the text in [[DesktopTextField|TextField1]] to the text file. |
This code appends the text in [[TextField|TextField1]] to the text file that was opened by [[FolderItem.ShowOpenFileDialog]]: | This code appends the text in [[TextField|TextField1]] to the text file that was opened by [[FolderItem.ShowOpenFileDialog]]: | ||
Line 64: | Line 64: | ||
If file <> Nil then | If file <> Nil then | ||
output = TextOutputStream.Open(file) | output = TextOutputStream.Open(file) | ||
− | output.Write(TextField1. | + | output.Write(TextField1.Text) |
output.Close | output.Close | ||
End If | End If | ||
Line 75: | Line 75: | ||
If file <> Nil Then | If file <> Nil Then | ||
output = TextOutputStream.Create(file) | output = TextOutputStream.Create(file) | ||
− | output.WriteLine(TextField1. | + | output.WriteLine(TextField1.Text) |
output.Close | output.Close | ||
End If | End If | ||
Line 81: | Line 81: | ||
== Sample Code == | == Sample Code == | ||
− | This code displays the Save As dialog box. A text file is then created and the text properties of three [[ | + | This code displays the Save As dialog box. A text file is then created and the text properties of three [[DesktopTextField|TextFields]] are written to the new file. Finally the file is closed. |
<rbcode> | <rbcode> | ||
Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt") | Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt") | ||
Line 87: | Line 87: | ||
Var fileStream As TextOutputStream | Var fileStream As TextOutputStream | ||
fileStream = TextOutputStream.Create(file) | fileStream = TextOutputStream.Create(file) | ||
− | fileStream.WriteLine(NameField. | + | fileStream.WriteLine(NameField.Text) |
− | fileStream.WriteLine(AddressField. | + | fileStream.WriteLine(AddressField.Text) |
− | fileStream.WriteLine(PhoneField. | + | fileStream.WriteLine(PhoneField.Text) |
fileStream.Close | fileStream.Close | ||
End If | End If |
Latest revision as of 22:32, 8 February 2022
In order to write text to a file, you need to create a TextOutputStream object. TextOutputStreams have methods that allow to write to a file and close the file when you are done writing to it. They are created by calling the Create and Append shared methods.
Properties | ||
|
Methods | |||||
|
Shared Methods | ||
|
Constructors | |
|
Interfaces
The TextOutputStream class implements the Writeable class interface.
Notes
Setting the Text Encoding
The default encoding is UTF8. If you want no encoding, use a BinaryStream. If you need to write a file using a different encoding, use the Encoding property before passing the text to the Write or WriteLine methods.
If documents <> Nil Then
Var file As FolderItem = Documents.Child("Sample.txt")
If file <> Nil Then
Try
// TextOutputStream.Create raises an IOException if it can't open the file for some reason.
Var output As TextOutputStream = TextOutputStream.Create(file)
output.Encoding = Encodings.WindowsANSI
output.Write(TextField1.Text)
output.Close
Catch e As IOException
// handle
End Try
End If
End If
All available encodings are in the Encodings module.
Creating and Appending to a Text File
Use Open when you want to open an existing text file and add text data to it. Use Create to write to a new text file. The following two examples illustrate the difference. Each example writes the text in TextField1 to the text file.
This code appends the text in TextField1 to the text file that was opened by FolderItem.ShowOpenFileDialog:
Var output As TextOutputStream
file = FolderItem.ShowOpenFileDialog(FileTypes1.Text)
If file <> Nil then
output = TextOutputStream.Open(file)
output.Write(TextField1.Text)
output.Close
End If
This code writes to a new text file.
Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "CreateExample.txt")
If file <> Nil Then
output = TextOutputStream.Create(file)
output.WriteLine(TextField1.Text)
output.Close
End If
Sample Code
This code displays the Save As dialog box. A text file is then created and the text properties of three TextFields are written to the new file. Finally the file is closed.
If file <> Nil Then
Var fileStream As TextOutputStream
fileStream = TextOutputStream.Create(file)
fileStream.WriteLine(NameField.Text)
fileStream.WriteLine(AddressField.Text)
fileStream.WriteLine(PhoneField.Text)
fileStream.Close
End If
See Also
BinaryStream, FolderItem, IOException, TextInputStream, TextOutputStream classes; ConvertEncoding function; Encodings module; Writeable class interface.