Working with text stream in REALbasic line

In this exciting tutorial we will learn some basics about saving, opening and convert encoding of text files in REALbasic using TextInputStream, TextOutputStream classes and ConvertEncoding function. Watch it, learn it, enjoy it!

Creating the GUI

Insert to Window Class one TextArea Control and 3 x PushButton Control. Here is my example of GUI look for this application. You can make it the same or insert the controls where you like.

Working with text stream in REALbasic

Save text to file

To save all text available in TextArea to text file insert to PushButton1 Sub Open() this code. Please have in mind that you can save the file without interacting with user for example: f = GetFolderItem(“textfile.txt”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dim t As TextOutputStream
Dim f As FolderItem
Dim dlg As SaveAsDialog
 
dlg = New SaveAsDialog 
 
f = dlg.ShowModal() // open save as dialog for entering the file name and selecting location to save
 
if f < > nil then // f different from nil
    t = f.CreateTextFile // create file in selected location
    t.Write (TextArea1.Text) // write date from TextArea to file
    t.Close // close file
else
    // user cancelled
end if

Open text from file

To open a text file and load it content to TextArea Control use this code in PushButton2 Sub Open().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
 
dlg = New OpenDialog 
 
f = dlg.ShowModal() // open open dialog for selecting a file to load
 
if f < > nil then // f different from nil
    t = f.OpenAsTextFile // open selected text file
    TextArea1.Text = t.ReadAll // read data from text file into TextArea control
    t.Close // close the file
else
    // user cancelled
end if

Convert text encoding

When you need to write text to a file that will be opened by other application that expects a particular encoding, you should convert the encoding before you call the Write method. You can do this by using ConvertEncoding function. Look on this example that converts the text from a TextArea1 to the UTF8 encoding. Code from PushButton3 Sub Open().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Dim t As TextOutputStream
Dim f As FolderItem
Dim dlg As SaveAsDialog
Dim s As String
 
dlg = New SaveAsDialog 
 
f = dlg.ShowModal() // open save as dialog for entering the file name and selecting location to save
 
if f < > nil then // f different from nil
    t = f.CreateTextFile // create file in selected location
    s = ConvertEncoding(TextArea1.Text, Encodings.UTF8) // convert encoding to UTF8
    t.Write (s) // write date from string to file
    t.Close // close file
else
    // user cancelled
end if

BinaryStream Class

BinaryStream objects are used to read and write data to and from a binary file. The benefit of using BinaryStreams rather than text streams is that you can read from and write to any position in the file. Text files must be read sequentially from the start to the end. On Windows and Macintosh only, BinaryStream objects can work with files larger than 2 gigabytes. 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Dim WriteToFile as BinaryStream
Dim ReadFromFile as BinaryStream
Dim f as FolderItem
 
f = GetOpenFolderItem("text")
 
if f < > nil then // if f different nil then
ReadFromFile = BinaryStream.Open (f,False)
ReadFromFile.littleEndian = True
f = GetSaveFolderItem ("","")
 
if f < > nil then // if f different nil then
WriteToFile = BinaryStream.Create (f,False)
while not ReadFromFile.EOF
WriteToFile.WriteShort ReadFromFile.ReadShort
wend
WriteToFile.Close
end if
ReadFromFile.Close
end if

Author: Jakub Pawlak

I'm interested in all. Currently working on the development of TUTSPOLIS and I hope that in near feature the project will be successful.

2 Responses to “Working with text stream in REALbasic”

  1. Steve says:

    CreateAsTextFile and OpenAsTextFile had been deprecated and soon will be removed. Use Stream instead

  2. Jakub Pawlak says:

    Thanks for tip Steve!

Leave a Reply

Copyrights (c) TUTSPOLIS | Powered by Wordpress MU | Inspired at Elegant Themes designed by qbap (HTML5 & CSS3)