Chapter 5. Apollo Mini-Cookbook
This chapter describes solutions to common tasks in Apollo applications. The solutions in this chapter illustrate many concepts described in previous chapters, and it provides working MXML code that you can test using Flex Builder or the Apollo SDK.
Working with the File System
Apollo lets you access the file system on the user’s computer. This section of the cookbook shows some solutions to common tasks when working with the file system.
For a conceptual overview of the Apollo file system capabilities, see “Accessing the File System.”
Writing a Text File from a String
Problem
You want to synchronously write data from an ActionScript String
object to a text file.
Solution
Call an appropriate write method, such as writeUTF( )
, of a FileStream object.
Discussion
This example uses a File
object named file
, which represents a test.txt file in the Apollo Test subdirectory of the user’s documents directory:
var file:File = File.documentsDirectory file = file.resolve("Apollo Test/test.txt");
For more information, see “Accessing the File System.”
The FileStream
class is used to open, read, write, and close files. You pass a fileMode
parameter to the open( )
method (or to the openAsync( )
method, if you want the operation to occur asyncronously) to specify the capabilities of the FileStream
object. In this case, FileMode.WRITE
provides write access to the file:
var stream:FileStream = new FileStream( ) stream.open(file, FileMode.WRITE);
Tip
The FileMode.APPEND
and FileMode.UPDATE ...
Get Apollo for Adobe Flex Developers Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.