UserGuide

iOS Files

From Xojo Documentation

The concepts described in UserGuide:File Access, UserGuide:Text Files and UserGuide:Binary Files also apply to iOS projects, but different classes are used there. This topic shows how the code in those sections looks for iOS projects.

Accessing Files

In iOS projects you use the Xojo.IO.FolderItem class instead of the FolderItem class. Functionality between these two classes is very similar, although some methods and properties may be slightly different.

Text Files

In iOS projects you use the Xojo.IO.TextInputStream class instead of the TextInputStream class to read text files and the Xojo.IO.TextOutputStream class instead of the TextOutputStream class to write to text files. Functionality between these classes is similar, although some methods and properties may be slightly different.

Reading from a Text File

This code reads a file from Documents and displays the text in a TextArea:

Var f As FolderItem = SpecialFolder.Documents.Child("MyFile.txt")
If f.Exists Then
Var input As TextInputStream
input = TextInputStream.Open(f, TextEncoding.UTF8)
TextArea1.Text = input.ReadAll
input.Close
End If

Writing to a Text File

This code creates a file in Documents and saves text from TextFields into the file:

Var file As FolderItem = SpecialFolder.Documents.Child("MyFile.txt")
If file <> Nil Then
Var output As TextOutputStream
output = TextOutputStream.Create(file, TextEncoding.UTF8)
output.WriteLine(NameField.Text)
output.WriteLine(AddressField.Text)
output.WriteLine(PhoneField.Text)
output.Close
End If

Binary Files

In iOS projects you use the Xojo.IO.BinaryStream class instead of the BinaryStream class. Functionality between these classes is similar, although some methods and properties may be slightly different.

Reading from a Binary File

This code reads a file made up of strings, and displays those strings in a TextArea:

Var f As FolderItem = SpecialFolder.Documents.Child("MyFile.data")
Var bStream As BinaryStream
bStream = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Var data As MemoryBlock
data = bStream.Read(bStream.Length)

bStream.Close

Writing to a Binary File

This code writes the contents of the TextArea1 to a text file:

Var f As FolderItem = SpecialFolder.Documents.Child("MyFile.data")
Var bStream As BinaryStream
bStream = BinaryStream.Create(f)

Var data As MemoryBlock
Var t As Text = "Text for the binary file."
data = TextEncoding.UTF8.ConvertTextToData(t)
bStream.Write(data)
bStream.Close

See Also

FolderItem, BinaryStream, TextInputStream, TextOutputStream classes